UIBarButtonItem不响应动作事件

时间:2014-01-25 17:36:52

标签: ios uibarbuttonitem uitoolbar

我正在使用UIToolBar,并为其添加了UIBarButtonItem。当我点击按钮时它什么也没做。它不响应动作事件。这是我的代码:

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(x, y, width, toolBarHeight)];
    [toolBar setBarStyle:UIBarStyleBlackTranslucent];
    UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                      style:UIBarButtonItemStyleBordered target:self action:@selector(changeText:)];
    toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
    barButtonDone.tintColor=[UIColor blackColor];
    [self.view addSubview:toolBar];

1 个答案:

答案 0 :(得分:0)

试试这个,看看这是否有帮助。你的代码再次看起来很好。

// create an array for the buttons
NSMutableArray* BarbuttonsArray = [[NSMutableArray alloc] initWithCapacity:1];

UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                              style:UIBarButtonItemStyleBordered target:self action:@selector(changeText:)];

barButtonDone.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray  addObject:barButtonDone];

[toolbar setItems:BarbuttonsArray  animated:YES];

我将您的代码复制并粘贴到我的测试项目中,这就是我得到的。我做的唯一改变是方法名称

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [toolBar setBarStyle:UIBarStyleDefault];
    UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                      style:UIBarButtonItemStyleBordered target:self action:@selector(changeText)];
    toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
    barButtonDone.tintColor=[UIColor blackColor];
    [self.view addSubview:toolBar];

}

-(IBAction) changeText
{
    NSLog(@"changeText ...");
}

2014-01-25 13:59:15.882 001-test-proj[15596:a0b] changeText ...