[英]Xcode: Changing my UIBarButton item title if pressed?
我试图根据是否单击按钮来设置UIBarbutton项目标题,我想我很接近,但不确定为什么它不起作用。 我正在使用一个情节提要,条形按钮的名称/方法是“ share1”,我所拥有的只是将其连接到情节提要上,这是到目前为止的代码:
在我的.h中:
- (IBAction)share1:(id)sender;
在我的.m中:
#import "ViewController2.h"
@interface ViewController2 ()
@property (assign, nonatomic) BOOL sharepressed;
@end
@implementation ViewController2
- (IBAction)share1:(id)sender {
//i am trying to open a menu in this but that shouldn't affect the button.
if (self.sideMenu.isOpen) {
self.sharepressed = YES;
[self.sideMenu close];
}
else {
[self.sideMenu open];
}
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Share" style:UIBarButtonItemStyleBordered target:self action:@selector(share1:)];
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (_sharepressed) {
self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Hide" style:UIBarButtonItemStyleBordered target:self action:@selector(share1:)];
_sharepressed |= _sharepressed;
}
}
任何帮助将不胜感激。
首先。 您可能要使用self.sharepressed
而不是_sharepressed
。 仅在初始化,设置器和获取器方法中使用第二种方法。 我建议您使用大写字母表示变量名中的新单词何时开始。 例如, sharepressed
使用sharePressed
而不使用sharePressed
,它特别是在名称真的很长的情况下使读取变得更加容易。
现在介绍您的代码。 您不想在操作发生时再次分配self.navigationItem.rightBarButtonItem
。 您可能想要做的就是更改标题。 最好使用这样的东西:
如果您使用的是导航控制器
self.title = @"Share";
如果您使用的是导航栏
self.navigationBar.title = @"Share"; // navigationBar is the name you gave to the object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.