如何将self分配给另一个类对象,同时导航到另一个没有segue的视图控制器

时间:2016-12-14 05:32:06

标签: ios objective-c uinavigationcontroller delegates uistoryboardsegue

我使用下面的代码导航到另一个视图控制器,我想将此self分配给另一个类的委托,我该怎么做?

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

UINavigationController *controller = [storyboard instantiateViewControllerWithIdentifier:@"storyBoardID"];

self.navigationController presentViewController:controller animated:YES completion:nil];

1 个答案:

答案 0 :(得分:0)

从导航控制器获取视图控制器

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

UINavigationController *controller = [storyboard instantiateViewControllerWithIdentifier:@"navigationControllerID"];

TargetVC *targetVC = (TargetVC *)controller.viewControllers.firstObject
   //OR
TargetVC *targetVC = (TargetVC *)controller.topViewController;

targetVC.mydelegate = self;

[self.navigationController presentViewController:controller animated:YES completion:nil];

从TabBar控制器获取视图控制器

   UITabBarController *homeTabBar=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"homeTabBarID"];

   TargetVC *targetVC =nil;

   UINavigationController *nav = [homeTabBar.viewControllers objectAtIndex:0];

  //if you have multiple tab then you can give Index as per your ViewController
   targetVC =(TargetVC*)[nav.viewControllers objectAtIndex:0];

   targetVC.mydelegate = self;

   [self.navigationController presentViewController:homeTabBar animated:YES completion:nil];