简体   繁体   English

iPhone-使用子视图中的按钮更改导航控制器的视图

[英]iPhone - change navigation controller's view, using a button in a subview

I have a navigation controller with a rootview. 我有一个带有rootview的导航控制器。 The rootview contains buttons, which push new views onto the navigation controller correctly. rootview包含按钮,这些按钮可将新视图正确地推送到导航控制器上。

However, on this rootview, I also have subview (it's a scrolling preview like the appStore images, view made of 3 UIview items). 但是,在此rootview上,我还有一个子视图(这是一个滚动预览,如appStore图像,由3个UIview项组成的视图)。 On these UIViews, there is a button which I'd like to change the rootview navigation controller, like the other buttons. 在这些UIView上,有一个我想更改rootview导航控制器的按钮,就像其他按钮一样。

At the moment, it builds and runs, but the new view is not pushed. 目前,它已构建并运行,但未推送新视图。 Any ideas? 有任何想法吗?

Click method on on the ScrollItem button: 单击ScrollItem按钮上的方法:

MyViewController *newView = [[MyViewController alloc] initWithNibName:@"ManageMyPain" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:newView animated:YES];  

... i guess it is because, self doesn't have a navigation controller, but the rootview does. ...我猜是因为,self没有导航控制器,但是rootview有。

Thanks!! 谢谢!!

The property "navigationController" is only being set in a view controller if he's pushed to the navigation controllers stack. 如果将属性“ navigationController”推送到导航控制器堆栈,则只能在视图控制器中设置该属性。 your second view is a sub view of a push viewcontroller which means this property is NULL and will not do anything if you try pushing things to it. 您的第二个视图是push viewcontroller的子视图,这意味着此属性为NULL,并且如果您尝试向其推送内容则不会执行任何操作。

Yes, it is because self.navigationController will be nil if you didn't push that controller on navigations stack or if you didn't set it manually. 是的,这是因为如果未在导航堆栈上推送该控制器或未手动设置该控制器,那么self.navigationController将为nil。

So you just need to have reference to rootViewController.navigationController and then [navigationController pushViewController:newView animated:YES]; 因此,您只需要引用rootViewController.navigationController ,然后再引用[navigationController pushViewController:newView animated:YES]; will work perfectly. 将完美地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM