Search code examples
iosobjective-cuistoryboardseguerootviewpoptoviewcontroller

Pass Data using popToRootViewController


So I have a basic app, here is how it works. I have a root view controller called A and a table view controller called B. And when the user selects a row in B I pop back to the root view controller A.

And what I am trying to do is to pass the data of the row that was selected as a NSString back to the root view controller A. And then use this string to "do something" depending on the string.

I have tried using the NSNotification method but then I can't use the string to do something.

Heres what I have tried:

//tableViewB.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passData" object:[[_objects objectAtIndex:indexPath.row] objectForKey:@"title"]];
    [self.navigationController popToRootViewControllerAnimated:YES];
}
//rootViewA.m
-(void)dataReceived:(NSNotification *)noti
{
     NSLog(@"dataReceived :%@", noti.object);

}
-(void)viewDidLoad {
  [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataReceived:) name:@"passData" object:nil];
}

What I am trying to do is some more like you can do when you push a viewController and use the perpareForSegue Method.

Thanks in advance for your help.


Solution

  • Try this may be help full

        MyAController *myController = (MyAController *)[self.navigationController.viewControllers objectAtIndex:0];
        myController.myText = @"My String" ;
        [self.navigationController popToViewController:myController animated:YES];
    

    I have use many time this .. It's working fine .. Note : replace your class name and string in this . Thanks :)