Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7592325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:52:43+00:00 2026-05-30T20:52:43+00:00

Why do I get a crash Terminating app due to uncaught exception ‘NSRangeException’, reason:

  • 0

Why do I get a crash "Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]" after setting rightBarButtonItem?

In my app, I have three buttons on the right hand side, one of which should alternate with the systemEditButton. So I set the buttons up withrightBarButtonItems (note the “s”) and then change the right-hand one using rightBarButtonItem when appropriate.

With 5.0, Apple lets you set multiple items in the leftBarButtonItems and rightBarButtonItems of a NavigationBar. It also says you can change the outer one with leftBarButtonItem and rightBarButtonItem respectively (“The first item in the array can also be set using the rightBarButtonItem property”).

Works fine the first time, but then it crashes when I put back the original button. Even worse, it doesn’t object when I set it, it crashes later during the animation of UINavigationBar layoutSubViews. Checking rightBarButtonItems after setting rightBarButtonItems shows it correctly updated the array, but it crashes during layout.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:52:44+00:00Added an answer on May 30, 2026 at 8:52 pm

    Well, I’ve confirmed this with a test program (see below), and submitted a bug-report to Apple. The workaround is to read the Items array and hand it back a new one with the zeroth element updated.

    //
    //  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;
    }
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get a crash with this console message: Terminating app due to uncaught exception
When my app crashes, I get a crash report that takes 5 minutes to
I get crash reports from my app. I really don't understand what the problem
I want to write to/delete a file but sometimes I get a crash if
When I run STLport on Darwin I get a strange crash. (Haven't seen it
I'm not entirely sure why this crash is happening and I'd like to get
I am trying to analyze a JVM crash that is occuring inconsistently. I get
I get a very confusing crash where I am currently running out of my
When i try to delete the cell i get crash ... Assertion failure in
I get the below given crash log when I try to use MFMailComposeViewController on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.