iOS storyboard passing data navigationViewController

别等时光非礼了梦想. 提交于 2019-12-17 10:26:18

问题


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

Picture describing my problem:

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?


回答1:


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...
}



回答2:


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...
}


来源:https://stackoverflow.com/questions/8429088/ios-storyboard-passing-data-navigationviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!