2012-03-05 197 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; 
} 
*/