gpt4 book ai didi

ios - 将 UIBarButtonItem 添加到推送到 UINavigation Controller 上的 UIViewController

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:11:09 24 4
gpt4 key购买 nike

我正在尝试将 UIViewController 推送到导航 Controller ,然后将 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];

当我使用上面的代码时,它不起作用。我也尝试从 Controller 本身的 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;

最佳答案

如果您转到 IB 并尝试添加一个 Flexible Space 或 Fixed Space,您将看到这 2 个对象的详细信息,表明这 2 个对象只能添加到正在添加到 UIToolBar 的 UIBarButtonItem,而不能到导航栏!

所以我建议您将代码更改为以下内容:

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

关于ios - 将 UIBarButtonItem 添加到推送到 UINavigation Controller 上的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15326544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com