2014-01-25 24 views
0

我正在使用UIToolBar並已將UIBarButtonItem添加到它。當我點擊按鈕時,它什麼都不做。它不響應行動事件。這是我的代碼: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]; 
+0

您是否說在點擊「完成」按鈕時不調用此類的'changeText:'方法? – rmaddy

+0

該代碼沒有太大的錯誤。如@rmaddy所示,檢查'changeText:'的功能。還要確保你還沒有將其他子視圖添加到覆蓋工具欄的'self.view'中。 – matt

+0

我首先添加選擇器和它上面的工具欄,這是我的changeText方法: - (void)changeText:(id)sender { int tag = [sender tag];如果(normalPicker){ [normalPicker removeFromSuperview]; } else { [datePicker removeFromSuperview]; } } –

回答

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 ... 
+0

問題是當我按下按鈕時,它不會像按下按鈕時看到的那樣處於按下狀態。我認爲這是問題。 –

+0

查看我更新的答案。如果你複製和粘貼這個確切的,它不起作用,那麼你的項目中還有其他事情正在進行。 –

+0

@DhruvJindal當然,在iOS 7中,按下的欄按鈕在默認情況下不會顯得特別按下。 – matt