我试图在导航栏右侧添加包含 2 个按钮的数组,但运行代码时出现异常。
'NSInvalidArgumentException',原因:'-[UIButton isSystemItem]:发送到实例的无法识别的选择器
我的代码确实非常简单:
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 模拟器上运行它。 任何想法?? 提前致谢。 铝
最佳答案
您不能直接添加UIButtons
。您需要首先将它们包装为 UIBarButtonItems
- 由于您仅传递一个数组,因此不会出现编译器警告。
使用initWithCustomView:
创建栏按钮项目,并将您的按钮作为自定义 View 传递。或者,根据按钮中的内容,直接创建栏按钮项目。
关于iphone - 将 UIButton 数组添加到 navigationItem.rightBarButtonItem 会导致 NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8279803/