如何将选定的数据传递回以前的视图控制器

我的function问题是我不知道如何传递我选择的数据,并回到上一个视图控制器。 链接是整个段落, http://pastebin.com/AtMjLD66 (对不起,我没有10个声誉,抱歉给您带来不便)欢呼。

我会建议代表,但如果你不想这样做,NSNotification会在这里节省你的一天。

可能在viewDidLoad提前添加了这个

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someThingSelectedInDetail:) name:@"someThingSelectedInDetail" object:nil]; - (void)someThingSelectedInDetail:(NSNotification*)notificationObject { NSString *chefName = notificationObject.object; NSLog(@"Chef Name from notification: %@",chefName); } 

并且在didSelect of advanceDetailVC.m中这样做。

 UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; [[NSNotificationCenter defaultCenter ]postNotificationName:@"someThingSelectedInDetail" object: cell.textLabel.text]; 

不是传递标签的文本,而是在这里有索引路径,所以直接从数组中取出它并在通知中传递它

 [[NSNotificationCenter defaultCenter ]postNotificationName:@"someThingSelectedInDetail" object: [detailArray objectAtIndex:indexPath.row]]; 

您可以使用委托模式将消息发送回视图控制器的委托(您可以设置为呈现视图控制器),传递有关选择所需的任何信息。

假设你想要它用于iOS,一个简单的电话:

 [[[UIApplication sharedApplication] delegate] doSomething]; 

或者由于您只有一个视图控制器,所以通用方式(与应用程序的设置方式无关):

 UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController]; 

您可以使用nsuserdefault来保存数据,然后您可以在上一课中找回