2012-03-05 149 views
0

为什么在设置rightBarButtonItem后碰到"Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]"为什么rightBarButtonItem崩溃?

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

使用5.0,Apple允许您在NavigationBarleftBarButtonItemsrightBarButtonItems中设置多个项目。它还表示,您可以分别更改leftBarButtonItemrightBarButtonItem(“阵列中的第一项也可以使用rightBarButtonItem属性设置)。

第一次正常工作,但当我放回原来的按钮时,它会崩溃。更糟的是,它在我设置它时并不反对,它在UINavigationBar layoutSubViews的动画期间后来崩溃。在设置rightBarButtonItems后,检查rightBarButtonItems显示它正确更新了该阵列,但在布局过程中崩溃。

+0

最好将此作为一个问题陈述(“为什么我会得到这个例外......”),然后自己发布答案。接受你自己的回答是很好的。 – 2012-03-05 21:59:36

+0

谢谢。我不清楚张贴的危险性质! – mackworth 2012-03-05 22:58:19

+0

不要忘记接受你的答案。 – 2012-03-05 23:44:32

回答

0

那么,我已经通过一个测试程序(见下面)证实了这一点,并向苹果公司提交了一个错误报告。解决方法是读取Items数组并将其更新为第零个元素并将其传回给新数组。

// 
// 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; 
} 
*/