如何将多个视图控制器推送到导航控制器上 [英] How to push multiple view controllers onto navigation controller
问题描述
新手在这里编写我的第一个应用程序(已经制作了几个教程应用程序).我使用名为RootViewController"的视图控制器作为导航控制器.我已使用以下命令成功地将另一个视图控制器推送到名为ClientListViewController"的顶部:
Newbie here programming my first app (having made several tutorial apps). I am using a view controller called 'RootViewController' as a navigation controller. I have successfully pushed another view controller on top of this called 'ClientListViewController' using the command:
[self.navigationController pushViewController:clientListViewController animated:YES];
我现在在 ClientListViewController 中并尝试将另一个视图控制器推送到名为AddClientViewController"的堆栈上.我想让它成为 UIModalPresentationFormSheet 形式的模态视图控制器.我正在尝试使用上述命令的变体来推送新的视图控制器,但我不知道如何替换自我".我试过了:
I am now in the ClientListViewController and trying to push another view controller onto the stack called 'AddClientViewController'. I would like to make this a modal view controller in the form of a UIModalPresentationFormSheet. I am trying to use a variation of the command above to push the new view controller but i don't know how to replace the 'self'. I have tried:
[RootViewController.navigationController pushViewController:AddClientViewController animated:YES];
还有……
[[RootViewController navigationController] pushViewController:AddClientViewController animated:YES];
以及这些组合中的每一个都使用小R"来表示词根.仍然没有运气.
as well as each of these combinations using small 'R's for the word Root. Still no luck.
为清楚起见,我在实现文件的顶部使用了以下代码.
For clarity, I have used the following code at the top of my implementation file.
#import "AddClientViewController.h"
我是否以正确的方式处理这个问题,还是应该使用全新的导航控制器将其添加到其中?
Am I approaching this in the right way, or should I be using a brand new navigation controller to add it to?
收到的任何指点.
非常感谢
推荐答案
每个 UIViewController
都有一个名为 navigationController
的属性.此属性指的是最近的封闭 UINavigationController
,如果有的话.所以你可以在你的 ClientListViewController
中说 self.navigationController
.
Every UIViewController
has a property named navigationController
. This property refers to the nearest enclosing UINavigationController
, if there is one. So you can just say self.navigationController
in your ClientListViewController
.
在 iOS 中,我们通常将类名大写.所以听起来 AddClientViewController
是一个类名.您需要有一个该类的实例才能将它推送到导航控制器的堆栈上.像这样:
In iOS, we normally capitalize class names. So it sounds like AddClientViewController
is a class name. You need to have an instance of that class to push it on the navigation controller's stack. Something like this:
AddClientViewController *addClientVC = [[AddClientViewController alloc] init];
[self.navigationController pushViewController:addClientViewController animated:YES];
在推送之前,您可能需要使用不同的 init 方法或设置 addClientVC
的某些属性;这取决于您对 AddClientViewController
的实现.
You might need to use a different init method or set some properties of addClientVC
before pushing it; that depends on your implementation of AddClientViewController
.
如果你想以模态的方式呈现它,你不要把它推送到导航控制器的堆栈上.相反,你这样做:
If you want to present it modally, you don't push it on the navigation controller's stack. Instead you do it this way:
AddClientViewController *addClientVC = [[AddClientViewController alloc] init];
addClientVC.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:addClientVC animated:YES completion:nil];
这篇关于如何将多个视图控制器推送到导航控制器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!