views:

1148

answers:

2

Hi,

This doesn't seem to be working. What am i doing wrong?

-(void)awakeFromNib{
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showNewEventViewController)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
NSLog(@"awaked");
[rightBarButtonItem release];

}
+3  A: 

I would normally put this code in the viewDidLoad method rather than the awakeFromNib method; I'm not sure if that's where your problem lies. What does "not working" mean?

Steve Harrison
Not working means, the button is not displaying in the nav bar.
Sam Jarman
Steve is right. Load it in -viewDidLoad instead. -awakeFromNib won't get called in a view controller--only in a UIView class.
Matt Long
A: 

The same problem. I'm trying to implement custom rightBarButtonItem for navigationBar. Here comes the code from my custom UINavigationController class:

-(void) viewDidLoad {
    [super viewDidLoad];

    [self initUI];
}

- (void) initUI {   
    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" 
                                                                  style:UIBarButtonItemStyleBordered 
                                                                 target:self 
                                                                 action:@selector(dismiss)];    

    self.navigationItem.rightBarButtonItem = btnCancel;

    [btnCancel release];
}

You see, I've put rightBarButtonItem initialization into viewDidLoad, as it was advised above. But, unfortunately, I hardly can find any buttons on navigationBar. Where is my mistake?

Thanks

NR4TR
resolved. i just added button in viewDidLoad of nested rootViewController
NR4TR