2013-04-27 82 views
1

In viewDidLoad: - (void)viewDidLoad {super viewDidLoad];UIBarButtonItem not firing its action method

[self.navigationController setNavigationBarHidden:YES animated:NO]; 

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init]; 
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Articles" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonTapped)]]; 
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Source" style:UIBarButtonItemStyleBordered target:self action:nil]]; 
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]]; 
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Aa" style:UIBarButtonItemStyleBordered target:self action:nil]]; 
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Rabbit" style:UIBarButtonItemStyleBordered target:self action:nil]]; 
[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; 
toolBar.items = toolBarItems; 
[self.view addSubview:toolBar]; 

方法:

- (void)backButtonTapped { 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

我對裏面的方法調用一個斷點,但它永遠不會被調用。爲什麼這個方法永遠不會被調用?

+0

你爲什麼要創建一個'UIToolbar'?爲什麼不使用導航控制器已經提供的工具欄?你在哪裏調用'self.toolbarItems = toolBarItems'? – rmaddy 2013-04-27 18:37:52

+0

因爲我需要不同於導航欄提供的功能,而UIToolBars是推薦我去做的事情的推薦方式。該呼叫進一步下降。 – 2013-04-27 19:36:12

+0

但是在什麼時候,您將創建的'toolBar'添加到導航控制器或視圖中?你有導航控制器嗎? – 2013-04-27 19:46:21

回答

5

我有一個UITapGestureRecognizer在整個視圖中截取UIBarButton上的水龍頭。我解決了它,歸功於this answer,它基本上停止了UITapGestureRecognizer的開始,除非它在UIToolBar之外。

1

您的代碼是不完整的,我不知道如果你不執行或者只是沒有寫在這裏,所以,以確保公正,這應該工作:

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init]; 
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Articles" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonTapped)]]; 
toolBar.items = toolBarItems; 
[self.view addSubview:toolBar]; 
+0

我附上完整的代碼。這仍然無法工作。 – 2013-04-28 14:48:11

0

嘗試這樣的。它的工作正常。

UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]initWithTitle:@"back"style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonTapped)]; 
    [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]]; 
    [self.view addSubview:toolbar]; 
+0

我附上完整的代碼。這仍然無法工作。 – 2013-04-28 14:48:54