Navigation Controller presented modally is empty 2nd time - ios

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.

Related

Unable to call navigation controller from present view controller in ios

In my application i am launching one screen using present UIModalViewController and on that screen I have oneUIButton, if we click on that UIButton alert will come then select yes on alert view now we have to call another view usingpushviewcontroller. But screen is not coming if we use below code can any one help me.
[self.navigationController pushViewController:requestViewController animated:YES];
Try with one root navigation controller and then present your controller modally as follows :
FirstViewController *firstView=[[FirstViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstView];
[self presentViewController:navigationController animated:YES completion:nil];
and then for push another view as follow :
SecondViewController *secondView=[[SecondViewController alloc]init];
[self.navigationController pushViewController:secondView animated:YES];
It will work first using present modal viewController and then using push navigation viewControllers on to the stack.

How to set root view controller of window to a UINavingationController when current root view controller of window is UITabBarController?

I have a UITabBarController which contains four navigation controllers.
When user taps a button , I want to set the root viewController of window to a new navigation controller.
How do I do that in Objective-C?
Using a modal view controller will look better than simply replacing the root view controller, and should produce the same effect as you are looking for. Basically, you create an instance of your view controller, add it to a new navigation controller, and then present the navigation controller modally:
- (void)buttonPressed
{
MyViewController *vc = [[MyViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nav animated:YES completion:nil];
}

Navigation Stack in IOS7

If I present a controller with a view controller, is it part of the self.navigationcontroller stack?
In essence:
UBSLoginViewController* loginView = [[UBSLoginViewController alloc] initWithNibName:LOGINVIEW bundle:nil];
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:loginView];
navigation.navigationBarHidden = YES;
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
[loginView presentViewController:[[UBSLoadingViewController alloc] initWithNibName:LOADINGVIEW bundle:nil] animated:YES completion:nil];
Is the loading view part of the navigation stack? Essentially, I want to present a modal view that will not be part of the root navigation stack.
If I present a controller with a view controller, is it part of the
self.navigationcontroller stack?
No.
You need to push the view controller onto your UINavigationController in order for it to be on the navigation controller's stack. Right now you are modally presenting on a view controller which is an entirely different concept.
Pushing onto a navigation controller looks something like this.
[self.navigationController pushViewController:loadingViewController animated:YES];

Instantiate UINavigationController on button action

I have simple application with only one main view, which has 'Settings' button, and settings are tree-grouped, so I wand to present them in navigation controller. And I don't want navigationController in main view, because I don't want navigation bar there.
That's why I don't instantiate navigationController in application: didFinishLaunchingWithOptions:. And when I check self.navigationController in 'Settings' button handler, it returns nil.
So I wrote this: (I use ARC)
- (void)doSettings
{
NSLog(#"%#", self.navigationController); // prints nothing
SettingsViewController *settingsViewController = [SettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *navigationController = [[UINavigationController alloc] init];
[self.view.window setRootViewController:navigationController];
[navigationController pushViewController:settingsViewController animated:YES];
}
This works, although it pushes settingsViewController without animation (don't know why).
Is this generally the correct way to do - to change rootViewController in the middle of running app?
And if yes - than when I'm done with Settings, I probably need to set rootViewController back to current viewController, as it was before I tapped 'Settings'?
I think you want to create a navigation controller that you will present modally; the following will do:
SettingsViewController* settingsViewController = [[SettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:settingsViewController animated:YES];
[self presentViewController: navigationController animated: YES completion:nil];
where self here is the view controller you want to trigger the modal view controller from.
since you present modally the navigation controller you can dismiss it within the code source of your settingsViewController by accessing its navigation controller:
[self.navigationController dismissViewControllerAnimated:YES completion: nil];
To answer your question setting the rootViewController is not the correct way. Present the new vc modally through the presentViewController method.
A better way is to build the navigation vc and present it over your main vc (not replace your main vc).
- (void)doSettings
{
NSLog(#"%#", self.navigationController); // prints nothing
SettingsViewController *settingsViewController = [SettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: settingsViewController];
[self presentViewController: navigationController animated:YES completion:^{}];
}
Your main vc might realize (maybe as a delegate) that the settings flow is complete. It can then dismiss the presented navigation controller with:
- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
Alternatively, the setting flow could dismiss itself...
// somewhere in the settings vc or a vc it pushes, when we decide settings are done
self.navigationController dismissViewControllerAnimated:YES completion:^{}];
Have navigation in main view and have below line (which will hide navigation bar)
[[self navigationController] setNavigationBarHidden:YES animated:YES];
(I would say have this in viewWillAppear and viewDidLoad both, BUT in viewWillAppear is MUST).
Now in second view, to show navigation bar
[[self navigationController] setNavigationBarHidden:NO animated:YES];
Hope this will solve your problem...

IOS: start navigation controller form a secondary view

In my app I have a first view controller and I open my secondview controller in this way:
[self presentModalViewController:puntiVendita animated:YES];
but when I'm in secondViewController I have a tableView and I want to begin a navigation controller...what can I do to start a navigation controller from seconviewcontroller via code?
thanks
Just create a navigation controller:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:puntiVendita];
[self presentViewController:navigationController animated:YES completion:nil];

Resources