The structure is as follows:
- View
- Tab Bar Controller
- Navigation Controller
- View Controller
- Navigation Controller
- View Controller
- Navigation Controller
- View Controller
- Navigation Controller
- View Controller
- Navigation Controller
- View Controller
- Navigation Controller
- View Controller
- Navigation Controller
The above controllers have been initialised in interface builder.
What I’m trying to do is add a right UIBarButtonItem to each navigation controller but not having any success.
Here’s what I’ve tried:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.view.opaque = NO;
self.tabBarController.view.frame = self.view.bounds;
NSArray *currentViewControllers = self.tabBarController.viewControllers;
NSMutableArray *updatedViewControllers = [NSMutableArray array];
for (int i=0; i<currentViewControllers.count; i++) {
UINavigationController *tempNav = [[UINavigationController alloc]init];
tempNav = [currentViewControllers objectAtIndex:i];
UIBarButtonItem *dismissButton = [[UIBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(dismissLibraryBrowser)];
tempNav.navigationItem.rightBarButtonItem = dismissButton;
[updatedViewControllers addObject:tempNav];
[dismissButton release];
[tempNav release];
NSLog(@"Added controller number %d",i);
}
self.tabBarController.viewControllers = [NSArray arrayWithArray:updatedViewControllers];
[self.view addSubview:tabBarController.view];
}
The code executes without any errors, but the button doesn’t appear. I’m sure I’ve misunderstood something here. Would appreciate some guidance.
You are over complicating things slightly with recreating viewControllers and temporary arrays. You just need to manipulate the objects loaded from the nib
As for the structure of your app – the docs for UITabBarController say
So I would suggest having a look at restructuring some stuff, if you need it only occasionally why not consider presenting it modally?