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;
}
Related
I have navigationController and I want to change nav back button text on appdelegate. so I want to change every uiviewController's back button's text on appdelegate. Is it possible?
I wrote the following code but it's not working.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
self.navigationController.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"custom back text"
style:UIBarButtonItemStyleBordered
target:nil
action:nil]; // didn't work
[self.window makeKeyAndVisible];
return YES;
}
You need to understand the difference between the navigation bar (there is one, owned, managed and controlled by the navigation controller) and the navigation items (there are many, one per view controller in the navigation controllers stack, owned and controlled by the view controllers). So you can't change it from where you are trying.
Instead, change it in each view controller or subclass the navigation controller (or act as its delegate) so you can change each view controller as its added to the stack.
Just adding to the answer that appeared while I was typing.
For example:
UIViewController *viewController = [[UIViewController alloc] init];
viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"custom back text"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[self.navigationController pushViewController:viewController];
But be careful. WIth this you're now responsible for handling the button action and this one here will do nothing.
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.
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.
I want to push root view controller. Why doesn't this code work?
RootController *rootController = [[RootController alloc]initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:rootController animated:YES];
[rootController release];
I used addSubview like this before.
- (void)cancel {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad { // this is root view controller
[super viewDidLoad];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}
RootController *rootController = [[RootController alloc]initWithStyle:UITableViewStylePlain];
UINavigationController *aNavigationController = [[UINavigationController alloc]initWithRootViewController:rootController];
self.naviController = aNavigationController;
[aNavigationController release];
[rootController release];
[self.view addSubview:[naviController view]];
And I added cancel button in navigation bar to go back to previous view. It doesn't work.
So, I want to push instead of add.
You set the root controller in a UINavigationController using the
initWithRootViewController:
method. So, the way you are doing is correct. I would suggest you to inspect self.view and ensure that it is not nil.
EDIT: after your comment
You need to define a root view controller for your UINavigationController to work properly; from the UINavigationController reference:
Every navigation stack must have at least one view controller to act as the root.
So you cannot remove the root view controller. Possibly, to make things work as you like you should create an additional view controller to use as root view controller that you do not alter, then push your RootViewController on the navigation stack, then popping would work:
UIViewController *baseController = [[UIViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:baseController];
self.naviController = aNavigationController;
[aNavigationController release];
[baseController release];
[self.view addSubview:[naviController view]];
RootController *rootController = [[RootController alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:rootController animated:YES];
[rootController release];
Notice that I first defined a simple UIViewController as root view controller, then pushed your controller on to it.
Once you do this, if you add the cancel button like you do, it will work popping the rootViewController from the navigation stack.
AFTER LAST COMMENT:
If I understand you right, when clicking on the cancel button, you want to get rid of the UINavigationController altogether.
In this case, use the following code for cancel:
- (void)cancel {
[self.navigationController.view removeFromSuperview];
}
If this guess is right, keep in mind that since you are not keeping any reference to the navigation controller, it will be deallocated and with it all the view controllers you instantiated.
If instead of removing the UINavigationController altogether, you would simpy hide the navigation bar, then after popping rootController, call:
setNavigationBarHidden:animated
- (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.
[window addSubview:viewController.view];
MainPageDialog *overviewViewController = [[MainPageDialog alloc] initWithNibName:#"MainPage" bundle:nil];
UINavigationController *nav_obj = [[UINavigationController alloc] initWithRootViewController:overviewViewController ];
[self.viewController presentModalViewController:nav_obj animated:YES];
[overviewViewController release];
[self.window makeKeyAndVisible];
return YES;
}
This code shows the blue bar of navigation controller, but no buttons on it.It seems like to be that the UINavigationController allocated as empty.
Who knows what problems is?
UPD:Archive http://www.mediafire.com/?lbjjvl6fcue2q18
Please help me, I'm new in objective-c
You need to create the button for it, for example:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:launcherView action:#selector(endEditing)];
self.navigationItem.leftBarButtonItem = doneButton;
[doneButton release];
The correct way to use a UINavigationController is to push view controllers on to it. That way they will be stacked and the navigation bar will be populated with a back button when it is case (i.e., when you can actually go back to a previous controller). You control the label that appears in the "back" button by defining the title of the controllers you push.
The technique shown in another answer (setting explicitly the button) is useful with defining the right button, if you ever need one.
You could try with this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MainPageDialog *overviewViewController = [[MainPageDialog alloc] initWithNibName:#"MainPage" bundle:nil];
UINavigationController* navigation = [[UINavigationController alloc] init];
[navigation pushViewController:overviewViewController animated:NO];
[overviewViewController release];
[window addSubview:[navigation view]];
[self.window makeKeyAndVisible];
return YES;
}
Instead of doing:
UINavigationController* navigation = [[UINavigationController alloc] init];
[navigation pushViewController:overviewViewController animated:NO];
you could also use initWithRootController, but to display the general case of how you push a view controller I preferred this one.
Notice that since you are pushing just a root controller, you should see no back button at the moment, but if you push a second view controller, then it will appear.
EDIT: I gave a look at your project. Summary of what you should try and do:
objects you need in your NIB: File's Owner (UIApplication), First Responder, FBFun App Delegate (iVkAppDelegate), Window (UIWindow); remove the rest;
File's owner delegate outlet is FBFun App Delegate;
FBFun App Delegate window outlet is Window.
With this simple setup (more or less what you have), use this code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController* navigation = [[UINavigationController alloc] init];
//-- MainPageDialog *overviewViewController = [[MainPageDialog alloc] initWithNibName:#"MainPage" bundle:nil];
iVkViewController *overviewViewController = [[iVkViewController alloc] init];
overviewViewController.title = #"First";
[navigation pushViewController:overviewViewController animated:NO];
iVkViewController *overviewViewController2 = [[iVkViewController alloc] init];
overviewViewController2.title = #"Second";
[navigation pushViewController:overviewViewController2 animated:NO];
[overviewViewController release];
[window addSubview:[navigation view]];
[self.window makeKeyAndVisible];
return YES;
}
In the code above, as you notice, I instantiated twice your iVkViewController just to have a second controller to push onto the navigator.
Please, delete your existing app from the simulator, and the run this in order to see that the navigation bar is correctly created and you can go back from the second controller to the first one.
I removed usage of MainPageDialog, because the MainPage nib has many problems.
But I hope this skeleton is sufficient for you to go forward with your development.
You had missed the line as you are not adding view to window.Add this line in your code
[window addSubview:nav_obj.view];