UIBarButtonItem not displayed - iphone

I can't make my UIBarButtonItem to display. I simplified it as much as I could, but even this does not display the bar :
- (void)viewDidLoad {
UIBarButtonItem *supprBouton =[[UIBarButtonItem alloc]
initWithTitle:#"Supprimer"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(supprimerDate:)];
self.navigationItem.rightBarButtonItem = supprBouton;
UIBarButtonItem *enregBouton = [[UIBarButtonItem alloc]
initWithTitle:#"Enregistrer"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(enregFichier:)];
self.navigationItem.leftBarButtonItem = enregBouton;
[enregBouton release];
[supprBouton release];
}
One right button, one left, this should be displayed at least, no ? What can be wrong ?

Without more context, my best guess is that you have not yet set up a UINavigationController.

Try to add a uinavigationcontroller to your view:
UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:vc];

Related

navigation bar is missing

Navigation bar is missing while pushing kalviewcontroller on button click.How to add navigation bar? so that it can be pop to view controller where its being pushed on button click.
kal = [[[KalViewController alloc] init]autorelease];
kal.title = #"Calendar of game";
kal.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:#selector(addEvent)] autorelease];
kal.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered target:self action:#selector(backToPrevious)]
autorelease];
kal.delegate = self;
dataSource = [self init];
kal.dataSource = dataSource;
[self.navigationController pushViewController:kal animated:YES];
Put [self.navigationController setNavigationBarHidden:FALSE] in you viewDidLoad(assuming you have created navigation controller).

how to add a rightbarbuttonitem in specific view of tabbarcontroller

as my title, how do i add in a rightbarbuttontiem in a specific view of tabbarcontroller ?
i'm using storyboard to create the tabarcontroller.
i tried this but the item are not displayed
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"gear.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(pushToSetting)];
//BarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = settingButton;
Are you using a NavigationController? If you are, then your code should work. If you are using just a UINavigationBar, then you have to set the items property for the UINavigationBar. As you are not getting a rightbuttonitem, I assume you are using a UINavigationBar. This code should work:
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStylePlain target:self action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Test"];
navItem.rightBarButtonItem = settingButton;
NSArray *navItems = [NSArray arrayWithObject:navItem];
[self.navBar setItems:navItems];
You should have the rightbarbuttonitem now. Here, navBar is a UINavigationBar outlet. You can also pop and push navigation items onto a UINavigationBar using these methods:
(void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
(UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;

Right button not appearing on modal view controller

I'm trying to add a button to a modal view controller so it can be dismised but the button is not displaying. Any help would be appreciated.
Here is the code:
UIViewController* webView = [[UIViewController alloc] init];
UINavigationController* modalViewController = [[UINavigationController alloc] initWithRootViewController:webView];
[webView release];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(hideModalView)];
modalViewController.navigationItem.rightBarButtonItem = doneButton;
[doneButton release];
[globalNavController presentModalViewController:modalViewController animated:YES];
You should do it like this.
webView.navigationItem.rightBarButtonItem = doneButton;
Try to add the doneButton to your UIViewController, then you should be fine.
Maybe you should change webView to webViewController, a better name.

Adding a Right "done" Button (UIBarButtonItem) to a UINavigationController

I see that a similar question was asked here: How to add a right button to a UINavigationController? (among others) but its not quite what I'm looking to do and they arent solving my problem.
Essentially, I've created a UIViewController called WebViewViewController with a UIWebView on it which will be shown using presentModalViewController. Essentially its a mini web browser to display a web page while keeping the user in the app rather than launching Safari.
The viewController does the following to get it to show... and the "done" button is meant to provide a place to close the browser.
-(IBAction)visitFacebook {
WebViewViewController *rootController = [[WebViewViewController alloc] init];
rootController.webURL = #"http://www.facebook.com/";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(done:)];
[navigationController.navigationItem setRightBarButtonItem:doneButton animated:YES];
[navigationController.navigationItem setTitle:#"Facebook"];
if (rootController) {
[self presentModalViewController:navigationController animated:YES];
}
[doneButton release];
[rootController release];
}
Unfortunately the "done" button isnt showing.. any ideas where im going wrong?
Try with below
-(IBAction)visitFacebook{
WebViewViewController *rootController = [[WebViewViewController alloc] init];
rootController.webURL = #"http://www.facebook.com/";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(done:)];
rootController.navigationItem.rightBarButtonItem = anotherButton;
[navigationController.navigationItem setTitle:#"Facebook"];
if (rootController) {
[self presentModalViewController:navigationController animated:YES];
}
[doneButton release];
[rootController release];
}
Perhaps you're looking for something more like this:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(dismissModalViewControllerAnimated:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self action:#selector(done:)];
Just this one line code displayed done button for me.

trying to programmatically create rightBarButtonItem

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];
}
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];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(dismiss)];
theRootController.navigationItem.rightBarButtonItem = btnCancel
[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];
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?
Try this instead:
- (void) initUI {
UIBarButtonItem *btnCancel = [[[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(dismiss)]autorelease];
self.navigationItem.rightBarButtonItem = btnCancel;
//[btnCancel release]; no need to explicitly release the item
}