从子视图导航控制器访问顶部导航控制器
我有一个像这样设置的视图和控制器。
- 1. 内的选项卡/栏控制器
- 是 2. 内的根视图控制器,
- 是一个以编程方式创建的导航控制器,在根视图控制器中显示为子视图。
我想做的是访问顶部选项卡栏/导航控制器,以便我可以将视图推送到上面。
我尝试了parentViewController,但它所做的只是将视图推送到编程的导航控制器上。
有什么建议吗?
这就是我设置根视图控制器的方式:
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"ROOT APPEARED");
[super viewDidAppear:animated];
WorklistViewController *worklistController = [[WorklistViewController alloc]initWithNibName:@"WorklistView" bundle:[NSBundle mainBundle]];
UINavigationController *worklistNavController = [[UINavigationController alloc] initWithRootViewController:worklistController];
worklistNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;
worklistNavController.view.frame = watchlistView.frame;
[worklistNavController.topViewController viewDidLoad];
[worklistNavController.topViewController viewWillAppear:YES];
[self.view addSubview:worklistNavController.view];
GetAlertRequestViewController *alertsController = [[GetAlertRequestViewController alloc]initWithNibName:@"AlertsView" bundle:[NSBundle mainBundle]];
UINavigationController *alertsNavController = [[UINavigationController alloc] initWithRootViewController:alertsController];
alertsNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;
alertsNavController.view.frame = alertsView.frame;
[alertsNavController.topViewController viewDidLoad];
[alertsNavController.topViewController viewWillAppear:YES];
[self.view addSubview:alertsNavController.view];
}
I have a my views and controllers set up like so.
- A Tab/Bar controller
- Within 1. is a root view controller
- within 2. is a programmatically created navigation controller, that is displayed as a subview in the root view controller.
What I am trying to do is access the top tab bar/navigation controller so that i can push a view onto it.
I tried parentViewController but all it did was push the view onto the programmed nav controller.
any suggestions?
This is how i set up my root view controller:
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"ROOT APPEARED");
[super viewDidAppear:animated];
WorklistViewController *worklistController = [[WorklistViewController alloc]initWithNibName:@"WorklistView" bundle:[NSBundle mainBundle]];
UINavigationController *worklistNavController = [[UINavigationController alloc] initWithRootViewController:worklistController];
worklistNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;
worklistNavController.view.frame = watchlistView.frame;
[worklistNavController.topViewController viewDidLoad];
[worklistNavController.topViewController viewWillAppear:YES];
[self.view addSubview:worklistNavController.view];
GetAlertRequestViewController *alertsController = [[GetAlertRequestViewController alloc]initWithNibName:@"AlertsView" bundle:[NSBundle mainBundle]];
UINavigationController *alertsNavController = [[UINavigationController alloc] initWithRootViewController:alertsController];
alertsNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;
alertsNavController.view.frame = alertsView.frame;
[alertsNavController.topViewController viewDidLoad];
[alertsNavController.topViewController viewWillAppear:YES];
[self.view addSubview:alertsNavController.view];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嵌套的 ViewController(即,在由实际上位于 NavController 堆栈上的 ViewController 控制的视图内)无法直接访问其父视图的控制器是其堆栈成员的 UINavigationController。这句话虽然拗口,但其含义是:你无法从这里到达那里。
相反,您必须通过应用程序委托访问应用程序的 NavController。
您正在使用 UIApplication 的单例(包含有关您的应用程序的各种有用信息),它有一个指向 AppDelegate 的 .delegate 属性,并且包含对 NavigationController 的引用。
无论如何,这就是“基于导航的应用程序”Xcode 模板设置 NavController 所有权的方式。 YMMV,如果你自己推出的话——不过如果你这样做了,你可能就不需要问这个问题了。
A nested ViewController (ie, inside a view controlled by a ViewController that's actually on the NavController stack) doesn't have direct access to the UINavigationController that its parent's view's controller is a stack member of. That's one MOUTHFUL of a sentence, but the sense of it is: you can't get there from here.
Instead you've got to get at the app's NavController via the App delegate.
You're using your UIApplication's singleton (contains all sorts of good info about your app), which has a .delegate property pointing to the AppDelegate, and that contains a reference to the NavigationController.
This is how the "Navigation-based Application" Xcode template sets up NavController ownership, anyway. YMMV if you rolled your own--though if you did, you probably wouldn't need to ask this question.
您可以使用以下说明:
它对我有用:D
You can use the follow instruccion:
It works for me :D
查看 UIViewController 的 navigationController 和 tabBarController 属性。这些将返回给定 UIViewController“所属”的相应 navigationController 或 tabBarController。
所以你可以这样做:
Have a look at UIViewController's navigationController and tabBarController properties. These will return the corresponding navigationController or tabBarController that the given UIViewController 'belongs' to.
So you can do something like: