I have a class RootViewController where I have a UIBarButtonItem declared. The method to display it is in another class FirstDetailViewController.
I am trying to access it in another class SecondDetailViewController, but it is always null. I tested with some other variables and they were null as well. Here’s what I have:
RootViewController.h
@interface RootViewController : UITableViewController <UISplitViewControllerDelegate> {
}
@property (nonatomic, retain) UIBarButtonItem *rootPopoverButtonItem;
...
@end
RootViewController.m
#import "RootViewController.h"
#import "FirstDetailViewController.h"
@implementation RootViewController
@synthesize popoverController, splitViewController, rootPopoverButtonItem;
- (void)splitViewController:(UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController:(UIPopoverController*)pc {
NSLog(@"splitviewController will hide");
// Keep references to the popover controller and the popover button, and tell the detail view controller to show the button.
barButtonItem.title = @"Menu";
self.popoverController = pc;
self.rootPopoverButtonItem = barButtonItem;
UIViewController <SubstitutableDetailViewController> *detailViewController = [splitViewController.viewControllers objectAtIndex:1];
[detailViewController showRootPopoverButtonItem:rootPopoverButtonItem];
}
FirstDetailViewController.m
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
NSLog(@"show root popover button item");
// Add the popover button to the toolbar.
NSMutableArray *itemsArray = [toolbar.items mutableCopy];
[itemsArray insertObject:barButtonItem atIndex:0];
[toolbar setItems:itemsArray animated:NO];
[itemsArray release];
}
SecondDetailViewController.h
#import "RootViewController.h"
@class RootViewController;
@interface SecondDetailViewController : UIViewController <SubstitutableDetailViewController, UIScrollViewDelegate, UITextFieldDelegate, UITextViewDelegate> {
...
}
@property (nonatomic, retain) RootViewController *root;
@end
SecondDetailViewController.m
#import "SecondDetailViewController.h"
@implementation SecondDetailViewController
@synthesize root;
...
NSLog(@"view class : %@", [root.splitViewController class]);
[detailViewController showRootPopoverButtonItem:root.rootPopoverButtonItem];
...
You probably aren’t setting the “root” property of SecondDetailViewController to the instance of RootViewController that you want to access the UIBarButtonItem for. Then you’re reading an uninitialized instance of RootViewController in your SecondDetailViewController code, and the only reason you don’t get an error is that Objective C silently ignores calls to methods on nil objects (in this case the rootPopoverButtonItem getter method, which the
root.rootPopoverButtonItemis shorthand for).If your instance of “RootViewController” is called “myRootViewController”, then somewhere in your code you have to do something like:
Then you’ll be accessing the copy of RootViewController that has the bar button you want.