I have the following code taken straight from the NavBar sample code from Apple. I put this in the viewDidLoad method for a view in my app that is being presented modally, and it wont work.
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"AddTitle", #"")
style:UIBarButtonItemStyleBordered
target:self
action:#selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
Any Suggestions?
Okay explained solution:
presentModalViewController:animated: presents a viewController modally, which does not have a UINavigationBar, so you can do some things:
Add a UINavigationBar in your viewController's nib and add the "Add" button there and everything you need to setup.
You can use pushViewController:animated: to show the viewController modally which will be on the navigation stack and have the UINavigationBar for you to add your button
If your first viewController is not a UINavigationController, using pushViewController:animated: won't solve it, so you can present a UINavigationController modally with your viewController as the rootViewController:
YourViewController *viewController =[[[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
[self.navigationController presentModalViewController:navController animated:YES];
Hope any of this helps
You need to use these lines of code on the page where you present the other view.
sceondController *obj=[[[sceondController alloc] initWithNibName:#"sceondController" bundle:nil] autorelease];
UINavigationController *navController=[[[UINavigationController alloc] initWithRootViewController:obj] autorelease];
[self.navigationController presentModalViewController:navController animated:NO];
and in second view use same code which you are using for making navigation button.
May be it resolves your problem.
I assume your view controller is actually a UINavigationController and everything else is in place. In which case I would change two things.
I wouldn't autorelease the UIBarButtonItem. That tends to be unreliable with view controllers so add the button to your list of things to dealloc at cleanup
I would use the setter function to set the button. Here is my code that works in my navigation controller
clearAllButton = [[UIBarButtonItem alloc] initWithTitle:#"Clear All" style:UIBarButtonItemStylePlain target:self action:#selector(rightButtonPressed:)];
[[self navigationItem] setRightBarButtonItem:clearAllButton];
Run your app in real device. In iOS6 it is not working on simulator.
Related
I'm using the following code to try to make a "back" button for my app, the view that this code is located is in a modal view (if that has any bearing?):
navBar = [[UINavigationController alloc] initWithRootViewController:tvController];
[navBar.view setFrame:CGRectMake(0, 0, 320, 460)];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: #"Back"
style: self.navigationController.navigationItem.leftBarButtonItem.style
target: self
action: #selector(backAction)];
navBar.navigationItem.backBarButtonItem.enabled = YES;
[self.view addSubview:navBar.view];
The view does not show at all, thank you for any tips!
EDIT: Even if I use a leftBarButtonItem, it still does not show up, I think there is some problem with the self.navigationItem bit of my code?
You need to make sure that when you present the modal view that you wrap it in a UINavigationController, then you'll have a valid navigation bar to manipulate. Otherwise you'll change the navigationItem all you want but it won't show up because you're not in a navigationController.
So when you go to present the view controller you're probably doing something like this.
SomeViewController *someViewController = [[[SomeViewController alloc] init] autorelease];
[self presentModalViewController:someViewController animated:YES];
What you want to do is present it like this
SomeViewController *someViewController = [[[SomeViewController alloc] init] autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:someViewController] autorelease]
[self presentModalViewController:navigationController animated:YES];
Then when you're in the modal view you'll have a valid navigation bar that you can manipulate. Altering the leftBarButtonItem at that point will actually do something and be visible.
If you're trying to make this show a back button though you're probably "doing it wrong" typically if you're presenting something modally like this you'd show a "done" button. However by wrapping this with a navigation controller like this it does allow the modal view to then push and pop view controllers and operate as a normal navigation stack. But the root of it should probably have a "done" button not a back to return back to its previous state.
The backBarButtonItem property needs to be defined on the previous item in your stack, i.e. on the view controller you are going back to, not the current one.
EDIT:
OK, I see now you are adding your own custom navigation bar. In that case, you cannot use the view controller's navigation item. You must instead push your own navigation items on to the navigation bar and access those instead. For example:
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Back"];
item.leftBarButtonItem = ...;
[navBar pushNavigationItem:item animated:NO];
Have tried all the examples on this website I just don't see anything on my modal view, I do see the navigationbar though but its empty
EditEntityViewController *editEntityViewController = [[EditEntityViewController alloc] init];
editEntityViewController.currentNode = newNode;
editEntityViewController.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editEntityViewController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show"
style:UIBarButtonItemStylePlain
target:self
action:#selector(refreshPropertyList:)];
editEntityViewController.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
[self presentModalViewController:navController animated:YES];
[editEntityViewController release];
As discussed, your code was correct and is the standard way to show a popup sheet with a UINavigationBar to hold buttons to dismiss the sheet. However, you had defined an IBOutlet in EditViewController called navigationItem, which was causing a conflict.
Try setting the rightBarButtonItem on editEntityViewController before you create the UINavigationController with initWithRootViewController:.
I think that the navigation bar is set up when the UINavigationController is created. Adding the right bar item after creation time is too late.
EDIT: Ok, so that's not the issue.
The following minimal code snippet works so I would check whether your EditEntityViewController is doing something to remove the button elsewhere:
- (IBAction)showPopup:(id)sender
{
UIViewController *popupController = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:popupController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show"
style:UIBarButtonItemStylePlain
target:self
action:nil];
popupController.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
[self presentModalViewController:navController animated:YES];
[popupController release];
}
The reason why this was not working is really stupid. Basically I had an IBOutlet defined in EditViewController called navigationItem which was conflicting with the SDK's property with the same name.
I removed it and the link from the nib and as Robin says it works perfectly.
Modally presented view controllers on navigation controllers don't have navigationItem nor navigationController properties. They DO, however, have parentViewController property, but this is irrelevant in your case.
If you want to customize navigation bar on your modally presented view, you should connect IBOutlet from view controller managing that view to the navigation bar placed in that managed view. Then do the manipulation through IBOutlet instance variable.
my application has a UIViewController subclass which is being managed by a UINavigationController.
In the viewDidLoad of my UIViewController subclass, I was attempting to add a UIBarButtonItem to the toolbar like this:
settingsButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings"
style:UIBarButtonItemStylePlain target:self action:#selector(viewSettings:)];
[self setToolbarItems:[NSArray arrayWithObject:settingsButton]];
this wasn't working out for me, so after some googling around, I tried this:
[[self navigationItem] setRightBarButtonItem:settingsButton];
which worked out fine. from reading the UIViewController documentation, I'm still confused about why setToolbarItems wasn't working. I verified in the debugger that the button was in the toolbarItems array in the viewDidAppear method. the button itself just wasn't appearing on my toolbar.
so, my question is, why didn't setToolbarItems work for me in the first code snippet?
I don't have the toolbar configured in my xib for this view controller at all, if that makes a difference.
Yes that make the difference.Whenever you see a bar on view by default for navigation based apps that is not a toolBar actually that is , navigation bar.so you can add item by referencing self.navigationItem.
[self setToolbarItems:[NSArray arrayWithObject:settingsButton]]; essentially populates the navigation controller's bottom toolbar - not the Left and Right top bar buttons.
The bottom toolbar is, by default, not displayed. To display it you must call [self.navigationController setToolbarHidden:NO]
Below is the relevant documentation - UINavigationController Class Reference
toolbar:
The custom toolbar associated with the navigation controller.
(read-only)
#property(nonatomic,readonly) UIToolbar *toolbar Discussion This
property contains a reference to the built-in toolbar managed by the
navigation controller. Access to this toolbar is provided solely for
clients that want to present an action sheet from the toolbar. You
should not modify the UIToolbar object directly.
Management of this toolbar’s contents is done through the custom view
controllers associated with this navigation controller. For each view
controller on the navigation stack, you can assign a custom set of
toolbar items using the setToolbarItems:animated: method of
UIViewController.
The visibility of this toolbar is controlled by the toolbarHidden
property. The toolbar also obeys the hidesBottomBarWhenPushed property
of the currently visible view controller and hides and shows itself
automatically as needed.
try to use
[toolbar setItems:[NSArray arrayWithObject:settingsButton] animated:YES];
instad of :
[self setToolbarItems:[NSArray arrayWithObject:settingsButton]];
shani
On ipad apps, you've got to set toolbar items to the "topViewController" (yes this is counter-intuitive).
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:catView];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"item 1" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"item 2" style:UIBarButtonItemStylePlain target:nil action:nil];
[nav setToolbarHidden:NO animated:YES];
// WRONG: [nav setToolbarItems:[NSArray arrayWithObjects:addButton, nil]];
// CORRECT (for ipad apps):
[nav.topViewController setToolbarItems:[NSArray arrayWithObjects:item1, item2, nil] animated:NO];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:nav];
In my app, I'm presenting a modalviewcontroller as follows and I'm not able to change the navigationbar's title or any of its properties for that matter.
fullListTopCompanies *fullListTopCompaniesInstance = [[fullListTopCompanies alloc] initWithNibName:#"fullListTopCompanies" bundle:nil];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:fullListTopCompaniesInstance];
[fullListTopCompaniesInstance setTitle:#"TEST"];
UIBarButtonItem *submit = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(displayViewForPosts)];
fullListTopCompaniesInstance.navigationItem.rightBarButtonItem = submit;
[submit release];
[self presentModalViewController:cntrol animated:YES];
[cntrol release];
I tried instantiating application delegate and assigning its navigationcontroller to local navigationcontroller instance but no use.
Somehow that navigationcontroller is not accessible. It can't be accessed by using "self.navigationitem". Whenever I present modalviewcontroller with the navigationcontroller, this navigation comes below the actual navigationcontroller.
For Example, if you are trying to set title of navigation bar for the ViewController called "ABCViewController", then add
self.Title = #"";
in viewWillAppear Method of the ABCViewController and try to rebuild and Run.
Hope this helps. :)
Whenever I present modalviewcontroller with the navigationcontroller, this navigation comes below the actual navigationcontroller.
That problem is because calling presentModalViewController: on self, you should call it on self.navigationController that way the navigation controller won't be shown below the other one.
As to why you can't set the navigationController's properties, I don't know. It looks Ok to me. But I expect it is because you are setting the properties before viewDidLoad is called by the nib-loader. I think I remember having problems like this myself a long time ago.
You should set the title etc. in the UIViewController subclass's viewDidLoad method and I think you worries will be over.
I've created a simple view based app with xcode template, then i've added your code and it's working for me...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
TestViewController *fullListTopCompaniesInstance = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:fullListTopCompaniesInstance];
[fullListTopCompaniesInstance setTitle:#"TEST"];
UIBarButtonItem *submit = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(displayViewForPosts)];
fullListTopCompaniesInstance.navigationItem.rightBarButtonItem = submit;
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[viewController presentModalViewController:cntrol animated:YES];
[cntrol release];
[submit release];
return YES;
}
In my app there is requirement that..I have 6 buttons in a nib,
when I press any button a new nib will be loaded into the window according to the button pressed. problem is after loading the new nib If I want to come back to the previous nib (which is having all the buttons) how to add navigation controller?
what I am doing now is while loading the new nib when I pressed the button
objNewViewController = [[NewViewController alloc] initWithNibName:#"NewViewController" bundle:nil];
[self.navigationController pushViewController:objNewViewController animated:YES];
but by this way im not able to load the nib, it's not performing any operation?
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[NewViewController alloc] initWithNibName:#"NewViewController" bundle:nil]];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
And in NewViewController:
USe this to dismiss and get back to previous view.
[[self navigationController] dismissModalViewControllerAnimated:YES];
There is a template in Xcode for a navigation based app. It does everything you describe. Well, very close at least, only the AnotherViewController in -tableView:didSelectRowAtIndexPath: is commented out.