rightbarbuttonitem not displaying


Answers

my guess is, that you add the UIBarButtonItem to the wrong object! you need to add it, to the rootViewController (instead to the UINavigationController, as you probably did)

YourRootViewController *theRootController = [[YourRootViewController alloc] init];

UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];

UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightbutton setBackgroundImage:[UIImage imageNamed:@"share-icon.png"] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
 theRootController.navigationItem.rightBarButtonItem = rightbutton;

[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];
Question

I have got following code for putting rightbarbuttonitem

UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightbutton setBackgroundImage:[UIImage imageNamed:@"share-icon.png"] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightbutton] autorelease];

But it is not displaying any barbuttonitem. Instead if I use following code then barbutton item appears but problem is i cant set touch event with this code on barbuttonitem.

UIImageView *iconView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dashboard-icon.png"]];
    UIBarButtonItem *icon = [[UIBarButtonItem alloc] initWithCustomView:iconView];
    self.navigationItem.leftBarButtonItem=icon;
    [icon release];
    [iconView release];



rightBarButtonItem not displaying on View

Check and Try

 - (void)viewDidLoad
{
    [super viewDidLoad];



    UIBarButtonItem *rigthButtonClicked=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rigthButtonClicked:)];

     [self.navigationItem setRightBarButtonItem:rigthButtonClicked];


     UIBarButtonItem *leftButtonClicked=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftButtonClicked:)];

 [self.navigationItem setRightBarButtonItem:leftButtonClicked];
}

Two Button Here

-(void)rigthButtonClicked:(UIBarButtonItem *)button
{

    NSLog(@"RigthButtonClicked");
}

-(void)leftButtonClicked:(UIBarButtonItem *)button
{


    NSLog(@"leftButtonClicked");
}




Tags