iphone - 将 UIButton 数组添加到 navigationItem.rightBarButtonItem 会导致 NSInvalidArgumentException

标签 iphone xcode cocoa-touch

我试图在导航栏右侧添加包含 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/

相关文章:

iphone - iOS 上 View 层级的可视化

iphone - iPhone 通用游戏(Cocos2d + Objective-C)中的断言错误,仅适用于 iPad 模拟器

iphone - 如何在 itunes connect 上离开团队?

ios - Pod 更新后 Github 丢失文件 (Xcode)

ios - 如何在iPhone SDK中使用手势在屏幕上绘图?

iphone - iPhone中Json数据加载缓慢

iphone - 一个好用的iPhone应用程序使用xcode开发教程

ios - 无法在 xib 中添加约束

iphone - 通过拖动重新排序 iPhone 上的 UITableView 部分(如重新排序行)

javascript - 是否可以在 Objective-C (iphone) 中使用 JavaScript?