向navigationItem.rightBarButtonItem添加一个UIButtons数组会导致NSInvalidArgumentException [英] Adding an array of UIButtons to navigationItem.rightBarButtonItem results in NSInvalidArgumentException

查看:157
本文介绍了向navigationItem.rightBarButtonItem添加一个UIButtons数组会导致NSInvalidArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在导航栏的右侧添加一个2个按钮的数组,但是当我运行代码时会出现异常。

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.

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

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

我的代码真的很简单:

   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];

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

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

推荐答案

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

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

使用 initWithCustomView:创建条按钮项,将按钮作为自定义视图传递。或者,根据您的按钮是什么,直接创建条按钮项。

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.

这篇关于向navigationItem.rightBarButtonItem添加一个UIButtons数组会导致NSInvalidArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆