2

我正在尝试将 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;
4

1 回答 1

3

如果你去 IB 并尝试添加一个灵活空间或固定空间,你会看到这 2 个对象的详细信息,说明这两个对象只能添加到正在添加到 UIToolBar 的 UIBarButtonItem,而不能添加到导航酒吧!

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

UIBarButtonItem *timerBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:nil action:nil];
于 2013-03-10T20:23:28.247 回答