Iphone 将UIButtons数组添加到navigationItem.rightBarButtonItem会导致NSInvalidArgumentException
我试图在导航栏的右侧添加一个包含2个按钮的数组,但在运行代码时出现异常 'NSInvalidArgumentException',原因:'-[UIButton isSystemItem]:未识别的选择器发送到实例 我的代码非常简单:Iphone 将UIButtons数组添加到navigationItem.rightBarButtonItem会导致NSInvalidArgumentException,iphone,xcode,cocoa-touch,Iphone,Xcode,Cocoa Touch,我试图在导航栏的右侧添加一个包含2个按钮的数组,但在运行代码时出现异常 'NSInvalidArgumentException',原因:'-[UIButton isSystemItem]:未识别的选择器发送到实例 我的代码非常简单: UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,45)]; label.backgroundColor=[UIColor clearColor]; label.
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您要给它一个对象数组,而不是UIButton对象。请注意,UIBarButtonItem既不继承UIButton,也不继承UIView。您不能直接添加
UIButtons
。首先需要将它们包装为uibarbuttonims
——因为您只传递了一个数组,所以没有编译器警告
使用
initWithCustomView:
创建条形按钮项,将按钮作为自定义视图传入。或者,根据按钮中的内容,直接创建条形按钮项 这里的技巧是使用uiBarButtonim对象而不是UIButton对象。UIBarButtonItems可以从UIButton创建,如下所示:
UIBarButtonItem *myItem = [[UIBarButtonItem alloc] initWithCustomView:uibuttonObject];
然而,在导航栏中使用UIButtons通常是个坏主意,因为UIBarButtonims在那里看起来很漂亮。考虑访问谢谢你的回复。我应该注意到这一点。非常简单:-)将
uibuttonItem
替换为UIBarButtonItem
,它就可以工作了。您不需要使用initWithCustomView
创建它。确保将UIBarButtonims数组分配给self.navigationItem.RightBarButtonims
,并在end@proca2.0是的,因此“或者,根据按钮中的内容,直接创建条形按钮项。”感谢您的实际拼写。