2012-03-05 3 views
0

rightBarButtonItemを設定した後、なぜクラッシュするのですか?"Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]"なぜrightBarButtonItemがクラッシュするのですか?

私のアプリでは、右側に3つのボタンがあり、そのうち1つはsystemEditButtonと交互に表示されます。そこで、私はrightBarButtonItems( "s"に注意してください)でボタンを設定し、適切な場合にはrightBarButtonItemを使用して右側のボタンを変更します。

5.0では、leftBarButtonItemsrightBarButtonItemsの複数の項目をNavigationBarに設定することができます。 leftBarButtonItemrightBarButtonItemで外側のものをそれぞれ変更することもできます(「rightBarButtonItemプロパティを使用して配列の最初の項目を設定することもできます」)。

初めて正常に動作しますが、元のボタンを戻すとクラッシュします。さらに悪いことに、私はそれを設定するとオブジェクトにならず、後でUINavigationBar layoutSubViewsのアニメーション中にクラッシュします。 rightBarButtonItemsrightBarButtonItemsを設定してチェックすると、正しく更新されたことがわかりますが、レイアウト中にクラッシュします。

+0

これを質問すると(「なぜこの例外が発生するのですか...」)、自分で回答を投稿することをお勧めします。あなたの質問に対するあなた自身の答えを受け入れることは大丈夫です。 –

+0

ありがとうございます。私は掲示のJeopardyのような性質が明らかではなかった! – mackworth

+0

あなたの答えを受け入れることを忘れないでください。 –

答えて

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; 
} 
*/ 
関連する問題