设置"Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]"
后,为什么会发生崩溃rightBarButtonItem
?
在我的应用中,我右侧有三个按钮,其中一个应与systemEditButton
交替显示。所以我用rightBarButtonItems
设置按钮(注意“s”),然后在适当的时候使用rightBarButtonItem更改右边的按钮。
使用5.0,Apple允许您在leftBarButtonItems
的{{1}}和rightBarButtonItems
中设置多个项目。它还说你可以分别用NavigationBar
和leftBarButtonItem
更改外部的那个(“数组中的第一项也可以使用rightBarButtonItem
属性设置”)。
第一次工作正常,但是当我放回原始按钮时它会崩溃。更糟糕的是,当我设置它时它不会反对,它会在rightBarButtonItem
的动画期间崩溃。设置UINavigationBar layoutSubViews
后检查rightBarButtonItems
表示它正确更新了阵列,但在布局过程中崩溃了。
答案 0 :(得分:0)
嗯,我已通过测试程序(见下文)证实了这一点,并向Apple提交了一份错误报告。解决方法是读取Items数组并将其更新为第0个更新的第0个元素。
//
// 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;
}
*/