Presenting a UINavigationController from a UIViewController doesn't have default cancel button - ios

I am trying to present a UINavigationController modally from a UIViewController. From previous experience, I'm pretty sure that when I do this there should be a cancel button by default in the navigation bar, however with the following code the navigation bar is completely blank. Any ideas?
UIViewController *rootVC = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootVC];
[self presentViewController:navController animated:YES completion:nil];

You will not get any bar button items by default. You need to either add the button before presenting, or, more correctly, add the button from inside the root view controller. Normally, you want to have delegation set up, where the presented view controller notifies the presenting view controller that it should dismiss it. Self-dismissing from inside the root view controller is usually not recommended.

– presentViewController:animated:completion: only going to present your view controller as a modal view controller, even you set up that as a navigation controller. the easiest solution in your case is in the navController set the left bar button as the cancel button, and call dismiss when click that button,
UIBarButtonItem *cancelButton= [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(dismissViewController)];
navController.navigationItem.leftBarButtonItem = cancelButton;
- (void)dismissViewController
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Hope that would help.

Related

Navigation Controller presented modally is empty 2nd time

I am trying to present a UITableViewController modally on one of my view controllers which is further embedded in a UITabBarController. The code I am using is -
ContactTableViewController *contactsVC=[self.storyboard instantiateViewControllerWithIdentifier:#"contact"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:contactsVC];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
contactsVC.myDelegate = self;
[self.navigationController presentViewController:navController animated:YES completion:nil];
The view is presented just fine the first time I do it but when I navigate to this view again in my app, the view being presented is completely blank.. No navigation bar buttons, no title, nothing..
Can't figure out a reason... Any help is very much appreciated!
UPDATE:
HomeViewController *vc = [[HomeViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
This what I do to navigate to that view again while unhiding the tab bar. Home View is the view that presents the Contacts Table View modally.

not able to move to another view controller

I'm trying to move from 1st view controller -> 2nd view controller. However, it seems nothing is happening.
It is mainly from a sign-in page, so I don't need to get back to that page once I have a successful login. I'm trying right now from a button just to check.
I have 2 classes.
signInPage
optionsScreen
I need to go from 1 > 2
Inside signInPage.m
#import "optionsScreen.h"
- (IBAction)moveToNext:(id)sender {
optionsScreen *aSecondPageController =
[[optionsScreen alloc]
initWithNibName:#"optionsScreen"
bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];
// [aSecondPageController release];
}
Screenshot of story board
https://drive.google.com/file/d/0B1y7ao4cGAYKaU0yRlVxcVg2bzA/edit?usp=sharing
Solution:
- (IBAction)moveToNext:(id)sender {
NSString * storyboardName = #"Main_iPhone";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"optionsScreen"];
[self presentViewController:vc animated:YES completion:nil];
}
In storyboard add navigation view controller. As root view controller - perhaps by default it is table view controller, so remove it - set up 1st view controller. Adding navigation controller you can push to navigation stack as many view controllers as you want.
I think what you're trying to do is present the view controller modally, not push it onto the stack (which doesn't exist). Instead you should just do this. This will not show a back button by default so you'll have to add one manually:
optionsScreen *aSecondPageController = [[optionsScreen alloc] initWithNibName:#"optionsScreen" bundle:nil];
[self presentViewController:aSecondPageController animated:YES completion:nil];
To go back, in your optionsScreen view controller, you'll need some sort of back button right? So you can do this in your viewDidLoad:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backPressed:);
Then you'll have to create a new method called backPressed:
-(void)backPressed:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
Edit: it turns out you were trying to do what I explained above. I will leave the navigation controller bit for future reference.
If you are trying to use a navigation controller to push your options, which will give you a back button by default, then first, in your storyboard, click your sign in view controller, then at the menu bar at the top go to Editor > Embed In... > Navigation Controller. Then in your view controller switch method, you can do what you were trying before
optionsScreen *aSecondPageController = [[optionsScreen alloc] initWithNibName:#"optionsScreen" bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];

Push Navigate from UITabbarController to UIViewController

I am using UITabbarController which has three child UIViewController named as FirstViewController, SecondViewController, ThirdViewController. I have a button on FirstViewController. When we tap the button, it need to push navigate to FourthViewController. Here the FourthViewController navigate inside UITabBarController itself. But i need it navigate out of UITabBarController not inside.
Button Target Action
ForthViewController *share = [[ForthViewController alloc] init];
[self.navigationController pushViewController:share animated:YES];
Could anyone guide me for right way. Thanks in Advance..
Try this:
ForthViewController *share = [[ForthViewController alloc] init];
[self.parentViewController.navigationController pushViewController:share animated:YES];
As long as your tab bar controller is in a navigation controller, this will push your view controller on that navigation stack.

iOS view controller memory not released after it's been dismissed

When the user clicks a button it presents a new tab bar view controller with two view controllers. Here's how I do that
ACLevelDownloadController *dvc = [[ACLevelDownloadController alloc] initWithNibName:#"ACLevelDownloadController" bundle:[NSBundle mainBundle]];
ACInstalledLevelsController *ivc = [[ACInstalledLevelsController alloc] initWithNibName:#"ACInstalledLevelsController" bundle:[NSBundle mainBundle]];
UITabBarController *control = [[UITabBarController alloc] init];
control.viewControllers = #[dvc, ivc];
dvc.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];
ivc.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:1];
[self presentViewController:control animated:YES completion:nil];
this works fine. I dismiss that view controller with a dismiss method in both the ACLevelDownloadController and ACInstalledLevelsController. That also works fine. What's strange is that the memory usage goes up when I present the view controller
but it never goes back down. If I present it again, it goes up even more
I'm using ARC. Why is the memory that the view controllers use not being released after they are dismissed?
EDIT
The way they are dismissed is both ACLevelDownloadController and ACInstalledLevelsController have IBActions hooked up that call this method when they are clicked
- (void)dismiss:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}
What we can observe from the memory usage graph is that the tabViewController is not being dismissed properly and it builds up in the stack. While dismissing you have to allow the viewController which presented the tabViewController to dismiss it. It is its responsibility to dismiss. Also keep weak references for Outlets and assign any strong references to nil** in viewWillDisapper: . You can present a viewController modally as a temporary interruption to obtain important information from the user. If its not the case here, you can remove presenting modally. Check this link. Hope this helps :)

Can a ViewController presented modally use the NavigationController's toolbar

I'm trying to present modally a UITableViewController from a view controller in my navigation controller hierarchy. The modal view should display a toolbar.
Can the navigation controller's managed toolbar be used in view controllers presented modally or should I implement my own toolbar for these?
If I present the controller modally with [self.navigationController presentModalViewController:filterVC animated:YES]; no toolbar is displayed.
If I pushed the controller with: [self.navigationController pushViewController:filterVC animated:YES]; the toolbar is displayed.
Here is the method I run from the init method of my UITableViewController.
-(void)configureToolBar {
[self.navigationController setToolbarHidden:NO animated:YES];
//ToolbarItem Done
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doneButtonPressed)];
//ToolbarItem Cancel
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelButtonPressed)];
//Flexible Space
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.toolbarItems = [NSArray arrayWithObjects:flexibleItem, cancelItem, doneItem, flexibleItem, nil];
[doneItem release];
[cancelItem release];
[flexibleItem release];
}
No, you can't, because a modal view controller becomes a child to the view controller that displays it, and this view controller is a subview of the navigation controller (i.e. the modal view has no connection with the NavigationController's hierarchy). You can present a navigation controller as a modal view controller, though, so you can pass your custom navbar buttons to it.
You have to embed your VC inside a new UINavigationController and set the toolbar items again or you just add a UIToolbar to the bottom of your VC.

Resources