我有一个UINavigationController
(使用类似于向导页面),这是我通过编程创建的,我需要显示一个“取消”按钮以取消任何UIViewController
中的过程。将相同的按钮添加到UINavigationController中的所有视图控制器中
创建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
,因为我需要在子视图中使用它。但我不知道如何将rightBarButtonItem
设置为以编程方式创建的UINavigationController
。
navigationController.topViewController.navigationItem.rightBarButtonItem = cancelButton;
有人可以点亮这个吗?
在此先感谢。
感谢您的澄清。我会去第三个选项并创建一个超类。 – Ozay
虽然是迟到的评论,但您应该避免通过超类向班级提供功能。在开发过程中,你可能会有一些不喜欢继承额外功能的控制器子类。不仅如此,你的代码可能会变得更加复杂。 –
感谢您对ViewControllers和NavigationItems之间关系的解释。 我的关于在segues(控制器开关)中存活的自定义按钮的方法将如下所示: * ViewControllers从BaseViewController继承。 * BaseViewController拥有NSMutableDictionary的属性 *此字典保持应显示在NavigationItem中的按钮的信息。 * BaseViewController更新当前的NavigationItems按钮组。 也许我的BaseViewController将通过协议连线。所以它会得到关于按钮配置的任何更改的通知。 – HBublitz