如何在呈现之前设置根视图控制器的属性?
How to set properties of a root view controller before being presented?
我在 storyboard
中有某个 UINavigationController
我从另一个视图控制器模态呈现:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:@"myNavController"];
[self presentViewController:myNavController animated:YES completion:nil];
此导航控制器已将另一个 UIViewController
设置为其在 storyboard
中的根视图控制器。我想在显示之前为这个根视图控制器设置一些属性,但我试过这个:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:@"myNavController"];
[self presentViewController:myNavController animated:YES completion:nil];
MyRootViewController *myRootViewController = [storyboard instantiateViewControllerWithIdentifier:@"myRootViewController"];
[myRootViewController setSelectedItem:selectedItem];
[myRootViewController setDelegate:self];
但是当我尝试设置其属性时,根视图控制器似乎尚未加载...
我该怎么做?谢谢
如果你调试过,你会发现MyRootViewController *myRootViewController = [storyboard instantiateViewControllerWithIdentifier:@"myRootViewController"];
制作的myRootViewController
并不是myNavController
真正的rootViewController。
这个方法-instantiateViewControllerWithIdentifier:
只是给你一个全新的 UIViewController 实例。
所以,如果你想获得真正的 rootViewController,只需将其替换为以下内容:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:@"myNavController"];
MyRootViewController *myRootViewController = myNavController.viewControllers[0];
[myRootViewController setSelectedItem:selectedItem];
[myRootViewController setDelegate:self];
[self presentViewController:myNavController animated:YES completion:nil];
我在 storyboard
中有某个 UINavigationController
我从另一个视图控制器模态呈现:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:@"myNavController"];
[self presentViewController:myNavController animated:YES completion:nil];
此导航控制器已将另一个 UIViewController
设置为其在 storyboard
中的根视图控制器。我想在显示之前为这个根视图控制器设置一些属性,但我试过这个:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:@"myNavController"];
[self presentViewController:myNavController animated:YES completion:nil];
MyRootViewController *myRootViewController = [storyboard instantiateViewControllerWithIdentifier:@"myRootViewController"];
[myRootViewController setSelectedItem:selectedItem];
[myRootViewController setDelegate:self];
但是当我尝试设置其属性时,根视图控制器似乎尚未加载...
我该怎么做?谢谢
如果你调试过,你会发现MyRootViewController *myRootViewController = [storyboard instantiateViewControllerWithIdentifier:@"myRootViewController"];
制作的myRootViewController
并不是myNavController
真正的rootViewController。
这个方法-instantiateViewControllerWithIdentifier:
只是给你一个全新的 UIViewController 实例。
所以,如果你想获得真正的 rootViewController,只需将其替换为以下内容:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:@"myNavController"];
MyRootViewController *myRootViewController = myNavController.viewControllers[0];
[myRootViewController setSelectedItem:selectedItem];
[myRootViewController setDelegate:self];
[self presentViewController:myNavController animated:YES completion:nil];