禁用RightBarButtonItems

时间:2013-11-16 02:04:41

标签: ios objective-c

我的viewController是一个容器,有自己的UINavigationBar。它不在导航控制器中。我的导航栏项目设置如此......

self.navigationItem.leftBarButtonItems = leftItems;
self.navigationItem.rightBarButtonItems = @[logout, settings];
[self.navBar setItems:@[self.navigationItem]];

在应用程序的各个点,此导航栏将被锁定,直到用户完成任务。此代码段适用于切换导航栏中按钮的启用属性,但仅适用于leftBarButtonItems!为什么呢?

for(UIBarButtonItem *rightButton in self.navigationItem.rightBarButtonItems){
     [rightButton setEnabled:!rightButton.enabled];
}
for(UIBarButtonItem *leftButton in self.navigationItem.leftBarButtonItems){
     [leftButton setEnabled:!leftButton.enabled];
}

1 个答案:

答案 0 :(得分:8)

<强>更新

我创建了一个测试演示,效果很好。这是sreenshots和代码,希望能给你一些帮助!

enter image description here enter image description here

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(nonatomic,strong) UINavigationItem * navItem;
@property(nonatomic,assign) IBOutlet UINavigationBar * navBar;

@end

ViewController.m

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem* barItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barItemClicked:)];
    UIBarButtonItem* barItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barItemClicked:)];
    UIBarButtonItem* barItem3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barItemClicked:)];
    UIBarButtonItem* barItem4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barItemClicked:)];
    self.navItem = [[UINavigationItem alloc] init];
    self.navItem.leftBarButtonItems = @[barItem1,barItem2];
    self.navItem.rightBarButtonItems = @[barItem3,barItem4];
    [self.navBar setItems:@[self.navItem]];

}
- (IBAction)anableSwitch:(id)sender{
    UISegmentedControl * swith = (UISegmentedControl *)sender;

    for(UIBarButtonItem *rightButton in self.navItem.leftBarButtonItems){
        [rightButton setEnabled:(swith.selectedSegmentIndex == 0)];
    }
    for(UIBarButtonItem *leftButton in self.navItem.rightBarButtonItems){
        [leftButton setEnabled:(swith.selectedSegmentIndex == 0)];
    }
}
- (void)barItemClicked:(id)sender{
    NSLog(@"barItemClicked");
}