How to pass the selected data back to previous vie

2019-07-12 09:05发布

My issue for my function is I am not sure how to pass the data which i selected, and back to the previous view controller. The link is to the whole paragraph,http://pastebin.com/AtMjLD66 (sorry I don't have 10 reputations, sorry about the inconvenience ) Cheer.

4条回答
forever°为你锁心
2楼-- · 2019-07-12 09:11

I would recommend delegates for this but if you don't want to do so, NSNotification would save your day here.

Add this is in advanceVC.m probably in 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);
}

And in didSelect of advanceDetailVC.m do this.

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

Instead of passing a label's text you have the indexpath here so take it directly from the array and pass it in the notification like this

[[NSNotificationCenter defaultCenter ]postNotificationName:@"someThingSelectedInDetail" object: [detailArray objectAtIndex:indexPath.row]]; 
查看更多
霸刀☆藐视天下
3楼-- · 2019-07-12 09:14

You can use nsuserdefault to save data and then you can retrieve back in the previous class

查看更多
Anthone
4楼-- · 2019-07-12 09:25

You would use the delegate pattern to send a message back to the view controller's delegate (which you could set to the presenting view controller), passing along whatever information you need to about the selection.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-07-12 09:27

Assuming you want it for iOS, a simple call :

[[[UIApplication sharedApplication] delegate] doSomething];

or Since you only have one view controller, the generic way (independent of how your app was set up):

UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController];
查看更多
登录 后发表回答