【问题标题】:Xcode: Changing my UIBarButton item title if pressed?Xcode:如果按下,更改我的 UIBarButton 项目标题?
【发布时间】:2013-07-23 23:40:17
【问题描述】:

我正在尝试根据按钮是否被单击来设置我的 UIBarbutton 项目标题,我认为我非常接近但不确定为什么它不起作用。我正在使用情节提要,barbuttons 名称/方法是“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;
    }
}

任何帮助将不胜感激。

【问题讨论】:

    标签: ios objective-c uibarbuttonitem


    【解决方案1】:

    首先。您可能想要使用self.sharepressed 而不是_sharepressed。仅在初始化器、设置器和获取器方法中使用第二种方式。我建议您使用大写字母来表示变量名中的新单词何时开始。例如,不要使用sharepressed,而是使用sharePressed,这样可以在名称很长时更容易阅读。

    现在关于您的代码。您不想在操作发生时再次分配self.navigationItem.rightBarButtonItem。您可能想要做的只是更改标题。最好使用这样的东西:

    • 如果您使用的是导航控制器

      self.title = @"Share";
      
    • 如果您使用的是导航栏

      self.navigationBar.title = @"Share"; // navigationBar is the name you gave to the object
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2011-03-19
      • 1970-01-01
      • 2013-02-12
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多