2

我試圖將UIViewController推送到導航控制器上,然後將UIBarButtonItem添加到導航欄中,以便我可以使用它來顯示倒數計時器。將UIBarButtonItem添加到推送到UINavigation控制器上的UIViewController

UINavigationController *navController = [[Session sharedInstance] getNavigationController]; 
GamePlayViewController *gameController = [[GamePlayViewController alloc] initWithNibName:@"GamePlayViewController" bundle:nil ]; 

UIBarButtonItem *timerBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

gameController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:timerBtn, nil]; 

[navController pushViewController:gameController animated:YES]; 

當我使用上面的代碼不起作用。我也嘗試從控制器本身的ViewDidload方法做到這一點,但沒有骰子。我甚至嘗試使用rightBarButtonItem卻得到一個錯誤,說明

"to uncaught exception 'NSInvalidArgumentException', reason: 'Fixed and flexible space items not allowed as individual navigation bar button item. Please use the leftBarButtonItems (that's plural) property.' 
*** First throw call stack:" 




UIBarButtonItem *timerBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

self.navigationItem.rightBarButtonItem = timerBtn; 

回答

3

如果你去IB並嘗試添加一個靈活的空間或固定的空間,你會看到這兩個對象,指出這2只能在細節被添加到正在添加到UIToolBar的UIBarButtonItem中,而不是添加到導航欄!

因此,我建議你改變你的代碼下面下手:

UIBarButtonItem *timerBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:nil action:nil]; 
+0

曾任職謝謝! – 2013-03-10 20:27:19

相關問題