[英]Dynamically change NavigationBar Back Button Text from presented ViewController
I'm trying to change the back button text on a UINavigationBar upon receiving something from the network - basically replicating iMessage/FB Messenger by adding a new message count to the back button text ie. 我试图在从网络接收到某些信息后更改UINavigationBar上的后退按钮文本-基本上是通过向后退按钮文本即添加新消息计数来复制iMessage / FB Messenger。 < Back (1) or < Messages (2)
<返回(1)或<消息(2)
I can change the back button text from the presented ViewController (in viewDidLoad) 我可以从显示的ViewController中更改后退按钮文本(在viewDidLoad中)
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Back";
self.navigationItem.backBarButtonItem = barButton;
But when I try to use that same code in another method that is called upon receiving a new message, the back button text doesn't change. 但是,当我尝试在收到新消息时调用的另一种方法中使用相同的代码时,后退按钮文本不会更改。
Any idea how I can change the back button text from the ViewController after it is presented ? 任何想法提出后,我如何能够改变来自ViewController中后退按钮的文字?
First of all, UINavigationItem.backBarButtonItem
for view controller A
is not the left bar button when A
is on top of the navigation stack, it is the left bar button when A
is second-top of the navigation stack. 首先,
UINavigationItem.backBarButtonItem
用于视图控制器A
不是当左栏按钮A
是对导航堆栈的顶部,它是左栏按钮时A
是导航堆栈的第二顶部 。
That is to say, you push A
, and then push B
, when B
is on top, the left bar button will be A.navigationItem.backBarButtonItem
也就是说,
A.navigationItem.backBarButtonItem
A
再按B
,当B
在顶部时,左栏按钮将为A.navigationItem.backBarButtonItem
So a easy but not so elegant solution would be: 因此,一个简单但不太优雅的解决方案是:
Give B
a weak reference to A
, and A
provide a interface to change it's back button like this: 给
B
弱引用A
, A
提供一个接口来更改它的后退按钮,如下所示:
@implementation A
-(void)changeBackTitle:(NSString*)title
{
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = back;
}
-(void)restoreBackButton
{
self.navigationItem.backBarButtonItem = nil;
}
@end
Then B
call this method to change the back button title when B
is on top of the stack 然后,当
B
位于堆栈顶部时, B
调用此方法以更改后退按钮标题
Remember to restore the back Button when B
is popped, otherwise next time push B
from A
will show a maybe wrong back button. 切记,当弹出
B
时,请恢复后退按钮,否则下次从A
按下B
,可能会显示出错误的后退按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.