首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么rightBarButtonItem会崩溃?

为什么rightBarButtonItem会崩溃?
EN

Stack Overflow用户
提问于 2012-03-06 00:45:55
回答 1查看 531关注 0票数 0

为什么设置rightBarButtonItem后会出现crash "Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]"

在我的应用程序中,我的右边有三个按钮,其中一个应该与systemEditButton交替使用。因此,我使用rightBarButtonItems设置按钮(注意"s"),然后在适当的时候使用rightBarButtonItem更改右边的按钮。

在5.0版本中,苹果允许你在NavigationBarleftBarButtonItemsrightBarButtonItems中设置多个项目。它还指出,您可以分别使用leftBarButtonItemrightBarButtonItem更改外部项(“也可以使用rightBarButtonItem属性设置数组中的第一项”)。

第一次运行正常,但当我放回原来的按钮时,它就崩溃了。更糟糕的是,当我设置它的时候,它没有反应,后来在UINavigationBar layoutSubViews的动画过程中崩溃了。在设置rightBarButtonItems之后检查rightBarButtonItems显示它正确地更新了数组,但在布局期间崩溃。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-06 06:59:42

好吧,我已经用一个测试程序证实了这一点(见下文),并向苹果提交了一份错误报告。解决方法是读取Items数组,并将第零个元素更新后的新数组返回给它。

代码语言:javascript
复制
//
//  ViewController.m
//  testBarButton
//
//  Created by Hugh Mackworth on 3/4/12.
//  Copyright (c) 2012 PineTree Software. All rights reserved.
//
#import "ViewController.h"
//#import <UIKit/UIKit.h>
//
//@interface ViewController : UIViewController {
//    
//@private
//    UIBarButtonItem * itemA;
//}
//
//@property (strong, nonatomic) UIBarButtonItem * itemA;
//
//@end

@implementation ViewController

@synthesize itemA;  //@property (strong, nonatomic) UIBarButtonItem * itemA;

-(void) reportButtons {
    NSLog (@"buttons:  %@",self.navigationItem.rightBarButtonItems);
}

-(void) itemA:(id) sender {
    NSLog(@"itemA hit");
   [self reportButtons];
}

-(void) itemB:(id) sender {
    NSLog(@"itemB hit");
    self.navigationItem.rightBarButtonItem = self.itemA;
    [self reportButtons];
}

-(void) itemC:(id) sender {
    NSLog(@"itemC hit");
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self reportButtons];
}

-(void) loadView {
    self.view = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    self.itemA = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Alternative Edit",@"")
                                                              style:UIBarButtonItemStyleBordered
                                                             target:self
                                                             action:@selector(itemA:)]; 
    UIBarButtonItem *itemB = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"B-Hit Second",@"")
                                                              style:UIBarButtonItemStyleBordered
                                                             target:self
                                                             action:@selector(itemB:)]; //fix with itemB2:
    UIBarButtonItem *itemC = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"C-Hit First",@"")
                                                              style:UIBarButtonItemStyleBordered
                                                             target:self
                                                             action:@selector(itemC:)]; //fix with itemC2:
    NSArray *rightItems = [NSArray arrayWithObjects: 
                           self.itemA,
                           itemB,
                           itemC,
                           nil];
    self.navigationItem.rightBarButtonItems = rightItems;
    [self reportButtons];                       

}

-(void) swapRightItem: (UIBarButtonItem *) newRightItem {
    NSMutableArray * newRightItems = [self.navigationItem.rightBarButtonItems mutableCopy];
    [newRightItems replaceObjectAtIndex:0 withObject: newRightItem];
    self.navigationItem.rightBarButtonItems = newRightItems;
    [self reportButtons];
}

-(void) itemB2:(id) sender {
    NSLog(@"itemB2 hit");
    [self swapRightItem:self.itemA];
}

-(void) itemC2:(id) sender {
    NSLog(@"itemC2 hit");
    [self swapRightItem:self.editButtonItem];
}

@end

//APP DELEGATE:
/*
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController * viewController = [[ViewController alloc] init];
    UINavigationController *watchNavController= [[UINavigationController alloc] initWithRootViewController:viewController ];

    self.window.rootViewController = watchNavController;
    [self.window makeKeyAndVisible];
    return YES;
}
*/
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9570464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档