xcode 将 UIButton 数组添加到 navigationItem.rightBarButtonItem 导致 NSInvalidArgumentException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8279803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:33:29  来源:igfitidea点击:

Adding an array of UIButtons to navigationItem.rightBarButtonItem results in NSInvalidArgumentException

iphonexcodecocoa-touch

提问by Alan

I am trying to add an array of 2 buttons to the right of a navigation bar, but I get a exception when I run the code.

我正在尝试在导航栏的右侧添加一个包含 2 个按钮的数组,但是在运行代码时出现异常。

'NSInvalidArgumentException', reason: '-[UIButton isSystemItem]: unrecognized selector sent to instance

'NSInvalidArgumentException',原因:'-[UIButton isSystemItem]:无法识别的选择器发送到实例

My code is pretty simple really:

我的代码非常简单:

   UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,45)];
   label.backgroundColor=[UIColor clearColor];
   label.text = @"Test 2 Buttons";

   UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
   button1.frame = CGRectMake(00.0f, 0.0f, 32.0f, 32.0f);

   UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
   button2.frame = CGRectMake(00.0f, 0.0f, 32.0f, 32.0f);

   NSArray *rightBarButtons = [[NSArray alloc] initWithObjects:button2, button1, nil];


   UINavigationItem* navItem = self.navigationItem;
   navItem.titleView = label;
   navItem.rightBarButtonItems = rightBarButtons;
   [rightBarButtons release];
   [label release];

I am running it on the iPhone 5.0 simulator. Any idea?? Thanks in advance. Al

我在 iPhone 5.0 模拟器上运行它。任何的想法??提前致谢。铝

回答by jrturton

You can't directly add UIButtons. You need to wrap them as UIBarButtonItemsfirst - there is no compiler warning since you are only passing an array.

不能直接加UIButtons。您需要UIBarButtonItems先将它们包装起来- 没有编译器警告,因为您只是传递一个数组。

Create the bar button items using initWithCustomView:, passing in your button as the custom view. Or, depending on what is in your buttons, create bar button items directly.

使用 来创建条形按钮项目initWithCustomView:,将您的按钮作为自定义视图传入。或者,根据按钮中的内容,直接创建条形按钮项目。

回答by aopsfan

The trick here is to use UIBarButtonItem objects instead of UIButton objects. UIBarButtonItems can be created from UIButtons like so:

这里的技巧是使用 UIBarButtonItem 对象而不是 UIButton 对象。UIBarButtonItems 可以从 UIButtons 创建,如下所示:

UIBarButtonItem *myItem = [[UIBarButtonItem alloc] initWithCustomView:uibuttonObject];

However, using UIButtons in a navigation bar is generally a bad idea, when UIBarButtonItems are meant to look pretty there. Consider visiting the UIBarButtonItem Class Reference.

然而,在导航栏中使用 UIButtons 通常是一个坏主意,因为 UIBarButtonItems 应该在那里看起来很漂亮。考虑访问UIBarButtonItem 类参考

回答by jbat100

You are meant to give it an array of UIBarButtonItemobjects not UIButton objects. Note that UIBarButtonItem inherits neither from UIButton nor UIView.

你应该给它一个UIBarButtonItem对象而不是 UIButton 对象的数组。请注意,UIBarButtonItem 既不继承自 UIButton,也不继承 UIView。