为什么设置rightBarButtonItem后会出现crash "Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]"?
在我的应用程序中,我的右边有三个按钮,其中一个应该与systemEditButton交替使用。因此,我使用rightBarButtonItems设置按钮(注意"s"),然后在适当的时候使用rightBarButtonItem更改右边的按钮。
在5.0版本中,苹果允许你在NavigationBar的leftBarButtonItems和rightBarButtonItems中设置多个项目。它还指出,您可以分别使用leftBarButtonItem和rightBarButtonItem更改外部项(“也可以使用rightBarButtonItem属性设置数组中的第一项”)。
第一次运行正常,但当我放回原来的按钮时,它就崩溃了。更糟糕的是,当我设置它的时候,它没有反应,后来在UINavigationBar layoutSubViews的动画过程中崩溃了。在设置rightBarButtonItems之后检查rightBarButtonItems显示它正确地更新了数组,但在布局期间崩溃。
发布于 2012-03-06 06:59:42
好吧,我已经用一个测试程序证实了这一点(见下文),并向苹果提交了一份错误报告。解决方法是读取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;
}
*/https://stackoverflow.com/questions/9570464
复制相似问题