将UIBarButtonItem添加到推送到UINavigation控制器上的UIViewController

时间:2013-03-10 19:30:58

标签: ios uinavigationcontroller uibarbuttonitem uitoolbar uinavigationitem

我正在尝试将UIViewController推送到导航控制器,然后将UIBarButtonItem添加到导航栏,以便我可以使用它来显示倒计时器。

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;

1 个答案:

答案 0 :(得分:3)

如果您转到IB并尝试添加灵活空间或固定空间,您将看到这两个对象的详细信息,这些对象表示这两个对象只能添加到添加到UIToolBar的UIBarButtonItem中,而不是到导航栏!

因此,我建议您将代码更改为以下内容以开始:

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