将相同的按钮添加到 UINavigationController 中的所有视图控制器
我有一个以编程方式创建的 UINavigationController
(像向导页面一样使用),我需要显示一个“取消”按钮来取消任何 UIViewController
中的进程。
创建 UINavigationController
:
FirstVC *firstVC = [[[FirstVC alloc] initWithNibName:@"FirstPage" bundle:nil] autorelease];
firstVC.delegate = self;
navigationController = [[UINavigationController alloc] initWithRootViewController:firstVC];
[self.view addSubview:navigationController.view];
添加取消按钮:
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelRequestNewLeave:)];
navigationController.topViewController.navigationItem.rightBarButtonItem = cancelButton;
[cancelButton release];
但是当我将第二个页面推送到 UINavigationController
时,取消按钮不显示显示在 UINavigationBar
上。如果我返回第一页,取消按钮就在那里。因此,显然该按钮仅针对第一个视图添加。我相信这是因为我没有子类化 UINavigationController
,因为我需要在子视图中使用它。但我不知道如何在以编程方式创建的 UINavigationController 中设置 rightBarButtonItem 。
navigationController.topViewController.navigationItem.rightBarButtonItem = cancelButton;
有人可以阐明这一点吗?
提前致谢。
I have a UINavigationController
(to use like a wizard page) which I create programmatically and I need to display a "Cancel" button to cancel the process in any UIViewController
.
Creating the UINavigationController
:
FirstVC *firstVC = [[[FirstVC alloc] initWithNibName:@"FirstPage" bundle:nil] autorelease];
firstVC.delegate = self;
navigationController = [[UINavigationController alloc] initWithRootViewController:firstVC];
[self.view addSubview:navigationController.view];
Adding Cancel Button:
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelRequestNewLeave:)];
navigationController.topViewController.navigationItem.rightBarButtonItem = cancelButton;
[cancelButton release];
But when I push a second page to UINavigationController
the cancel button is not shown on the UINavigationBar
. If I go back to first page, the cancel button is there. So, apparently the button is added only for the first view. I believe this is because I'm not subclassing UINavigationController
, because I need to use it in a subview. But I don't know how to set the rightBarButtonItem
in a UINavigationController
which is created programmatically.
navigationController.topViewController.navigationItem.rightBarButtonItem = cancelButton;
Can someone shed a light on this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
导航项是每个视图控制器的。导航栏从当前正在构建其视图的视图控制器的导航项中绘制其内容,该视图控制器对应于导航控制器堆栈顶部的视图控制器。
您基本上需要每个视图控制器在其导航项中粘贴一个取消按钮。您可以执行以下任一操作:
The navigation item is per view controller. The navigation bar draws its contents from the navigation item of the view controller whose view it's currently framing, which corresponds to the view controller at the top of the navigation controller's stack.
You basically need each view controller to stick a cancel button in its navigation item. You can do any of the following:
您还可以子类
UINavigationcontroller
并覆盖一些方法,如下所示:You can also subclass
UINavigationcontroller
and overide few methods like this:您可以在创建
UINavigationController
实例的类中采用UINavigationControllerDelegate
协议。您还可以提前创建cancelButton
,然后像这样实现navigationController:willShowViewController:animated:
,您必须记住创建并按住
cancelButton
code> 并且不释放它。这也意味着cancelRequestNewLeave:
必须是类中的一个方法,用于创建UINavigationController
实例,我猜这就是现在的情况。You can instead adopt the
UINavigationControllerDelegate
protocol in the class which creates theUINavigationController
instance. You can also create thecancelButton
in advance and then implementnavigationController:willShowViewController:animated:
like this,You will have to remember to create and hold the
cancelButton
and not release it. This will also meancancelRequestNewLeave:
will have to be a method in class that creates theUINavigationController
instance which is what it is right now I guess.像这样
CommonViewController .h
CommonViewController.m
FirstViewController.h
FirstViewController.m
SecondViewController.h
SecondViewController.m
注意:可以在CommonViewController的viewDidLoad中添加initializeCartBarButton的代码,并从CommonViewController及其子类中删除该函数
like that
CommonViewController.h
CommonViewController.m
FirstViewController.h
FirstViewController.m
SecondViewController.h
SecondViewController.m
note: you can add the code of initializeCartBarButton in the viewDidLoad of CommonViewController and delete this fuction from CommonViewController and from child class's
这就是我使用
UINavigationController
子类实现的方法,该子类能够消除推入其中的每个 viewController。This is how I did it with
UINavigationController
subclass that is capable of dismissing every viewController pushed into it.您需要在每个视图控制器中添加按钮。您不能通过设置一次或在视图控制器之间共享一个(以合理的方式)来做到这一点。添加按钮的好地方是视图控制器的 viewDidLoad 方法。如果您觉得这有点重复,您可以为它们创建一个基本的 UIViewConteoller 子类。
You'll need to add the button in every view controller. You cannot do it by setting one once or sharing one between view controllers (in a sensible fashion). A good place to add the button is in the viewDidLoad method of your view controllers. You can create one basic UIViewConteoller subclass for them if you feel this gets to repetitive.
您可以将自定义“取消”UIButton 直接添加到导航栏的视图中,而不是使用 UIBarButtonItem。
执行此操作的正常方法是将取消按钮添加到导航堆栈中每个视图控制器的 navigationItem 中。上述方法可以让您编写更少的代码,从而使其变得更简单,但它是一个小技巧。
You can add a custom 'Cancel' UIButton directly to the NavigationBar's view instead of using the UIBarButtonItem.
The normal way to do this is to add that cancel button to the navigationItem of every single view controller in your navigation stack. The above approach can make it simpler by allowing you to write less code, but it is a tiny bit of a hack.
将此代码添加到您的 rootview viewDidLoad 方法中,并在 rootview 控制器中实现 cancelMethod。这将在所有视图控制器中可用。您可以通过更改按钮框架来调整按钮位置。对于方向更改,您必须手动调整按钮的位置。
Add this code in your rootview viewDidLoad method and implement the cancelMethod in rootview controller.This will be available in all the view controllers. you can adjust the button location by changing button frame.For orientation change you have manually adjust the location of button.