ios在一个segue中将值传递给另一个视图

试图做我见过的很多地方logging的东西,但似乎无法pipe理。 我试图在一个segue期间将数据从一个视图传递到另一个视图。

以下是源视图中的prepareForSegue方法:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { //pass values DetailViewController *dest = (DetailViewController *)[segue destinationViewController]; dest.locTitle = [value valueForKeyPath:@"title"]; dest.locInfo = [value valueForKeyPath:@"info"]; // NSLog(@"The title is: %@, and the info is: %@.",dest.locTitle,dest.locInfo); } 

如果我启用最后的NSLog ,我看到正确的值…

以下是目标视图上的界面:

 #import <UIKit/UIKit.h> @interface DetailViewController : UIViewController{ } @property (strong, nonatomic) NSString *locTitle; @property (strong, nonatomic) NSString *locInfo; @property (weak, nonatomic) IBOutlet UILabel *detailMainText; @property (weak, nonatomic) IBOutlet UILabel *detailTitle; @end 

…这里的viewDidLoad和相关的目标视图@synthesize:

 @synthesize detailMainText,detailTitle,locTitle,locInfo; - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"In viewDidLoad - the title is: %@ and the info is: %@",self.locTitle,self.locInfo); detailTitle.text = locTitle; detailMainText.text = locInfo; } 

NSLog报告每个variables的null值。

我会很感激你可以给我的任何帮助。 我确定我在做一些愚蠢的事情,很明显。

在此先感谢您的帮助。


很久以后…


好的…我想我正在把它缩小。

我发现(反直觉地)

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

被调用

 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

这是令人困惑的,也弄糊涂了我的小计划,找出我的详细信息视图中应显示我的NSDictionary数据源的块。 目前我正在这样做:

 // send info to detail view according to which row selected - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //Get the selected object in order to fill out the detail view myValue = [myLocations objectForKey:[locationsIndex objectAtIndex:indexPath.row]]; } 

但是,因为这发生在赛后 ,这是没用的。 如何访问prepareForSegue方法中的行号?

再次感谢。


最后…


以下是这个问题的答案:

如何在prepareForSegue方法中访问选定的行号?

希望有人会发现它有用。

首先,在listView控制器的界面中为tableview设置一个IBOutlet

 @property (weak, nonatomic) IBOutlet UITableView *tableView; 

那么你的prepareForSegue可以做到这一点:

 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString: @"showDetail"]) { //pass values NSLog(@"The sender is %@",sender); NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; //Get the selected object in order to fill out the detail view id myValue = [myLocations objectForKey:[locationsIndex objectAtIndex:indexPath.row]]; DetailViewController *dest = [segue destinationViewController]; dest.locTitle = [myValue valueForKeyPath:@"title"]; dest.locInfo = [myValue valueForKeyPath:@"info"]; //NSLog(@"The title is: %@, and the info is: %@.",dest.locTitle,dest.locInfo); } } 

你可以这样做:首先传递你想要发送到目标控制器的值:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"segueToLocation" sender:the object you want to pass]; } 

然后在这里获取对象

 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString: @"segueToLocation"]) { DetailViewController *dest = (DetailViewController *)[segue destinationViewController]; //the sender is what you pass into the previous method dest.target=sender } } 

您不需要创build目标视图控制器的本地实例variables。 这也会做同样的事情:

 [segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"title"]; [segue.destinationViewController setLocInfo: [myValue valueForKeyPath:@"info"]; 

另一种select – 可以使用以下方法:

– (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath :(NSIndexPath *)indexPath

在prepareForSegue之前发生。

你可以试试这个:

 [[NSString alloc] initWithFormat:@"%@", [segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"title"]]; [[NSString alloc] initWithFormat:@"%@", [segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"info"]]; 

我想我有同样的问题,所以希望我的解决scheme也能帮助你。 它应该回答你的问题的第一部分,或者至less会帮助别人。

首先,您不能在SecondViewController中将传递的值赋给带有viewDidLoad方法的属性 。 这样做,给nil,因为viewDidLoad是在Segue之前实现的。

而且我不会推荐使用viewDidAppear ,因为在视图加载之后值会被改变,使得更改过程在视觉上可见。

所以,最好的select是直接将值分配给IBOutlet's 。 如果你想传递一个string数组,你可以先分配一个你想加载的数组值,同时也传递所有的NSArray 。 这将允许您的视图已经加载的价值,也将有其他值的工作。

您也可以调用方法并传递数据。