“self.navigationItem.rightBarButtonItem”不工作
我有以下代码直接取自 Apple 的 NavBar 示例代码。我将其放入我的应用程序中以模态方式呈现的视图的 viewDidLoad 方法中,但它不起作用。
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"AddTitle", @"")
style:UIBarButtonItemStyleBordered
target:self
action:@selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
有什么建议吗?
I have the following code taken straight from the NavBar sample code from Apple. I put this in the viewDidLoad method for a view in my app that is being presented modally, and it wont work.
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"AddTitle", @"")
style:UIBarButtonItemStyleBordered
target:self
action:@selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
Any Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的解释解决方案:
presentModalViewController:animated:以模态方式呈现一个viewController,它没有
UINavigationBar
,因此您可以做一些事情:UINavigationBar
并添加那里的“添加”按钮以及您需要设置的一切。pushViewController:animated
: 以模态方式显示将位于导航堆栈上的 viewController,并使用UINavigationBar
供您添加按钮(UINavigationController
,使用pushViewController:animated:
无法解决该问题,因此您可以以 viewController 作为模态呈现UINavigationController
rootViewController:希望这些有帮助
Okay explained solution:
presentModalViewController:animated: presents a viewController modally, which does not have a
UINavigationBar
, so you can do some things:UINavigationBar
in your viewController's nib and add the "Add" button there and everything you need to setup.pushViewController:animated
: to show the viewController modally which will be on the navigation stack and have theUINavigationBar
for you to add your buttonUINavigationController
, usingpushViewController:animated:
won't solve it, so you can present aUINavigationController
modally with your viewController as the rootViewController:Hope any of this helps
您需要在呈现其他视图的页面上使用这些代码行。
并在第二个视图中使用与制作导航按钮相同的代码。
也许它可以解决您的问题。
You need to use these lines of code on the page where you present the other view.
and in second view use same code which you are using for making navigation button.
May be it resolves your problem.
我假设你的视图控制器实际上是一个 UINavigationController 并且其他一切都已就位。在这种情况下,我会改变两件事。
我不会自动释放 UIBarButtonItem。对于视图控制器来说,这往往不可靠,因此将按钮添加到要在清理时释放的事物列表中
我将使用 setter 函数来设置按钮。这是我在导航控制器中运行的代码
clearAllButton = [[UIBarButtonItem alloc] initWithTitle:@"全部清除" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonPressed:)];
[[self navigationItem] setRightBarButtonItem:clearAllButton];
I assume your view controller is actually a UINavigationController and everything else is in place. In which case I would change two things.
I wouldn't autorelease the UIBarButtonItem. That tends to be unreliable with view controllers so add the button to your list of things to dealloc at cleanup
I would use the setter function to set the button. Here is my code that works in my navigation controller
clearAllButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear All" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonPressed:)];
[[self navigationItem] setRightBarButtonItem:clearAllButton];
在真实设备中运行您的应用程序。在 iOS6 中,它无法在模拟器上运行。
Run your app in real device. In iOS6 it is not working on simulator.