My view controller will open modal view that is a UITableView subclass. UITableView subclass dont have navigation bar. So how can I add it programmatically ? Because I need cancel button on it to close this modal view.
Add a table view controller to a navigation controller, and present the navigation controller modally. For example (this is all pseudo-code as I'm not sure of the exact method names):
MyTableViewController *tvc = [[MyTableViewController alloc] init];
tvc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" target:self action:#selector(dismissModalViewController)];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tvc]];
[self presentModalViewController:navController];
If you want it to push the view with a navigation bar and back button and you are using a TableView controller, then why not just use a NavigationController to push it since this will give you the navigation bar and back button by default?
Related
I have a ViewController subclass that I'm presenting as a modal view:
MyViewController *myViewController = [[MyViewController alloc] init];
[self presentModalViewController:myViewController animated:YES];
However, the modal view showing up with a white stripe on the bottom, approximately the height of the title or navigation bar. My app is otherwise navigation controlled. How do I get rid of the white stripe (and ideally add a title bar)?
I'm not sure if this is one of two things from your question, are you trying to add a navigation controller to your modal view controller, or are you trying to hide your navigation controller from your modal view.
I suspect you may want to in fact change your code to the following to present your modal view controller from your NavigationController
MyViewController *myViewController = [[MyViewController alloc] init];
[[self navigationController] presentModalViewController:myViewController animated:YES];
Then call setTitle: on you MyViewController object to set the title for it.
When I try to present a modalViewController, it covers up my navigation controller's navigation bar. Any tips? Thanks.
UPDATE (with code):
ComposeText *compText = [[ComposeText alloc] initWithNibName:#"ComposeText" bundle:[NSBundle mainBundle]];
compNavController = [[UINavigationController alloc] initWithRootViewController:compText];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:compNavController action:#selector(compDoneTapped:)];
compNavController.navigationItem.rightBarButtonItem = doneButton;
[self presentModalViewController:compNavController animated:YES];
compText.title = #"Compose";
[doneButton release];
Everything seems to be in order, but the button is still not appearing on the navigation bar.
That's what it is supposed to do. If you want a navigation bar, present a new UINavigationController modally and set it's root view controller to your modalViewController.
The question you need to ask yourself is: "why do I want to show my navigationbar". If it is to give the user access to some buttons then it is the wrong reason. Modal view controllers are there to take full control of the screen and to not allow the user to manipulate anything else in the app until the controller is dismissed. If you don't want that do as Cyprian suggests and push a viewcontroller on your navigation stack.
If it is just a visual thing (logo ...) duplicate it in your modal view controller.
UINavigationController has it's own method to show another viewController.
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
It uses stack, to push new viewController so it can handle its navigation with buttons.
When using the modalViewController, I found that that only way to keep a title bar and buttons on the screen that I jump to was to create a navigation controller and set the modalView screen as the rootView of the navigation controller (as below).
userNameViewController = [[UserNameViewController alloc] init];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:userNameViewController];
[self.navigationController presentModalViewController:cntrol animated:YES];
[userNameViewController release];
[cntrol release];
Why can't I simply this by using the original viewController as the modalView (as below)?
userNameViewController = [[UserNameViewController alloc] init];
[self presentModalViewController:userNameViewController animated:YES];
Because the navigation bar you usually see belongs to the navigation controller, not to its children view controllers.
You could create your own navigation bar and add it to your controller's view. However, it would require some relayout (and that's probably why it's easier to just recreate a navigation controller).
I have a very simple question. I have a view controller. In the view controller, i have added few buttons. On button click I have to start a tab bar controller (each of which has navigation controller).
Code Snippet:
- (IBAction) pushNewsAlertsController:(id)sender {
//Create a navigation controller
UINavigationController *FirstNavController = [[UINavigationController alloc] init] ;
//create the view controller (table view controller)
FirstViewController *firstViewController = [[FirstViewController alloc] init];
[FirstNavController pushViewController:firstViewController animated:YES];
//Create a navigation controller
UINavigationController *secondNavController = [[UINavigationController alloc] init] ;
//create the view controller (table view controller)
SecondViewController *secondViewController = [[SecondViewController alloc] init];
[secondNavController pushViewController:secondViewController animated:YES];
// Set the array of view controllers
tabBarController.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, nil] ;
//Add the tab bar controller's view to the window
[self.view addSubview:tabBarController.view];
}
I am able to add the tabviews and navigation controller. The problem is I am unable to get back to the main view once the button is clicked.
Can anyone guide me on how to get back to the previous view so that I can click other buttons.
In this case, I would consider presenting the tab bar controller modally. It's a cleaner way of organizing your views in the same way the user interface is organized. You can just dismiss the modal presentation to go back to the previous view. Read View Controller Programming Guide for iOS about similar advice and examples.
Not sure if I get what you are trying to do, but if you want the tab bar controller view to disappear again, the only way would be the inverse of
[self.view addSubview:tabBarController.view];
which could be
[tabBarController.view removeFromSuperview];
I must say it looks like an odd construction you're building, but it may work nevertheless.
I Have a view with a button. I want to press the button and that a Navigation Controller will appear, and in it there will be a back button that will bring me to the original view.
this is how i load the Navigation Controller:
-(IBAction) viewButtonPressed:(id) sender
{
[self.view addSubview:navController.view];
[self.view bringSubviewToFront:navController.view];
}
in the rootController of the NAvagtion Controller I tried this, but it does not work (the back button does not even appear):
#implementation rootViewController
- (void)viewDidLoad {
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(customGoBack:)];
}
navigationController is the stack of viewControllers. You didn't push any controller except root to this stack, so there is no back button in the navigationBar. You can create separate controller to push it into the navigationController's stack. If you son't want to see navigationBar on your first controller's view, just send - (void)setNavigationBarHidden:YES animated:NO to your instance of UINavigationController.
Use this code,
YourViewController *yvController= [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:yvController animated:NO];
Its the simplest way to navigate on different views.