31

I have problem with proper passing data between view's but not in standard way.

Picture describing my problem:

https://i.stack.imgur.com/0jHYC.png

I performSegueWithIdentifier with one of two segue identifiers and then in I want to pass data to ViewController called "Firmy" or "Oddzialy".

Passing data code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([[segue identifier] isEqualToString:@"sLogowanieFirmy"]) {
      FirmyVC *firmyVC = [segue destinationViewController];
      firmyVC.tabFirmy = self.tabFirmy;
  }
  if ([[segue identifier] isEqualToString:@"sLogowanieOddzialy"]) {
      OddzialyVC *oddzialyVC = [segue destinationViewController];
      oddzialyVC.wybranaFirma = [self.tabFirmy objectAtIndex:0];
  }
}

Problem is with method [segue destinationViewController] becouse destinationViewController for segue is NavigationViewController.

So what is proper way to pass data and have independent Navigation Controllers?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sliwinski.lukas
  • 1,412
  • 1
  • 18
  • 28
  • Maybe you want to take a look at the `NSNotificationCenter` class. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html – dimme Dec 08 '11 at 09:59
  • There is no more convenient way? – sliwinski.lukas Dec 08 '11 at 10:14

2 Answers2

58

UINavigationController has a property called topViewController which returns the view controller that is at the top of the stack.

So your prepareForSegue: method may look something like this...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"sLogowanieFirmy"]) {
        UINavigationController *nav = [segue destinationViewController];
        FirmyVC *firmyVC = (FirmyVC *)nav.topViewController;
        firmyVC.tabFirmy = self.tabFirmy;
    }

    // etc...
}
Mark Adams
  • 30,776
  • 11
  • 77
  • 77
3

Here it is in swift:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{    
    if (segue.identifier == "sLogowanieFirmy") {
        let nav = segue.destinationViewController as! UINavigationController 
        let firmyVC = nav.topViewController as! FirmyVC
        firmyVC.tabFirmy = self.tabFirmy            
    }

    // etc...
}
mmd1080
  • 1,812
  • 2
  • 17
  • 29