dismiss rootViewController on button tap - objective-c

I have a viewController instantiated by storyboard on application launch which is a part of hybrid app. On top of that I have presented one more view controller. Now While dismissing the presentedViewController I want to dismiss the rootViewController as well so as to show the hybrid app screen. How can I achieve this?

[self dismissViewControllerAnimated:YES completion:^{
// after your second view controller dismissed.
// set your new view controller as a root of window.
// You need to set navigation controller and set any root view controller for that navigation controller in storyboard.
// also don't forget to set identifier of your navigation controller.
UINavigationController* rootController = [[UIStoryboard storyboardWithName:kStoryboardName bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"controllerIdentifier"];
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
window.rootViewController = rootController;
// this will set your new navigation controller with root view on UIWindow.
}];

OK, I don't know anything about Worklight. Or what a hybrid app is. So this may not make sense. But here's a strictly iOS answer to your question.
It doesn't really make sense to dismiss the root view controller without replacing it with another view controller. If you do, you would leave your app with no way to interact with it (except maybe shaking the phone?).
So there isn't a way to dismiss it as you can with child view controllers. But you can just remove it.
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[keyWindow.rootViewController.view removeFromSuperview];
keyWindow.rootViewController = nil;

NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
[viewControllers removeObjectAtIndex:0];
[self.navigationController setViewControllers:viewControllers];

Related

presentViewController over TabBarController causes "attempt to present *VC on TabBarVC whose view is not in the window hierarchy"

My root view controller is a Tab Bar Controller loaded in the delegate. Each of the tabs is a table view controller. When the application is first loaded I want a login screen to popup via presentViewController, which I have in my viewDidLoad method of the Tab Bar Controller. It results in the window hierarchy error that I have read about - though none of the solutions have worked for me. I tried instead presenting the modal view in the viewDidLoad method of the first tab but this resulted in the same error.
In my tab bar view controller in viewDidLoad, my code is:
CLLoginViewController *loginVC = [[CLLoginViewController alloc] init];
loginVC.delegate = self;
[self.view addSubview:loginVC.view];
[self presentViewController:loginVC animated:NO completion:nil];
Your question is quite vague but what i have understood, You want to present a login screen before the tabController is loaded.
So a better way would be, In your AppDelegate when setting rootViewController of window, do something like this
if([[NSUserDefaults standardUserDefaults] boolForKey:#"showTabBar"]){
self.window.rootViewController = tabBarControllerObject; //TabBarView is loaded
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"showTabBar"];
}
else{
self.window.rootViewController = tabBarControllerObject; //Login Screen is loaded for first time.
}
Hope this helps

removeFromParentViewController doesnot update UINavigationbar

I want to remove all the viewcontrollers from UINavigationController. So I am using this code.
for (UIViewController* controller in navigationController.viewControllers) {
[controller removeFromParentViewController];
}
After that I create an new viewController and push it.
UIViewController* newVC=[[UIViewController alloc] init];
[navigationController pushViewController:newVC animated:YES];
Issue is all the viewcontrollers popout perfectly and adding newVC but on pushing newVC the navigationbar is getting a back button and title of newVC. On clicking back button it animates to the navigationbar of oldVC with title of oldVC that I have already removed in above loop;
removeFromParentViewController is a UIViewController method, so it's normal it has nothing to do with UINavigationBar
In the case of a UINavigationController the popViewControllerAnimated: method handles the removeFromParentViewControllerpart for you, along with navigation bar.
you can directly update the whole array of viewControllersof UINavigationController, calling `setViewControllers:animated:
see Replacing rootView in navigationController
[navigationController setViewControllers:[NSArray arrayWithObject:newVC]];

How to presentModalViewController without dismiss the TabBarController

Hey guys i`m trying to present a modal view controller inside an application with a tab bar controller. The problem is, every time the new view is presented, it on top of my tab bar.
I need to keep my tab bar even when the view is presented. Like google maps application does with his toolbar at the bottom of the screen.
How can i do that?
Thank you
By default, a modal view controller is meant to take up the entire screen (on an iPhone/iPod, at least). Because of this, it covers whatever you have on screen at the time.
A view controller presented via modal segue is meant to live on its own. If you want to keep your Navigation and TabBar, then just use a push segue to present the new ViewController. Remember to use this kind of segue, your presenting controller needs to be part of a UINavigationController already.
Use this to push a ViewController. If it is a UINavigationController it will push its linked RootViewController by itsself.
Create a viewController to push: (Using Storyboard)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
or (Using Code/Nibs)
LoginViewController *viewController = [[LoginViewController alloc] init]; //initWithNibNamed in case you are using nibs.
//in case you want to start a new Navigation: UINavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
and push with:
[self.navigationController pushViewController:vc animated:true];
Also, if you are using Storyboards for the segues you can use this to do all the stuff. Remember to set the segue identifier.
[self performSegueWithIdentifier:#"pushLoginViewController" sender:self]; //Segue needs to exist and to be linked with the performing controller. Only use this if you need to trigger the segue with coder rather than an interface object.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"pushLiftDetail"]) {
[[segue.destinationViewController someMethod:]];
segue.destinationViewController.someProperty = x;
}
}
I think you'll need to add a UITabBar to the modal view and implement/duplicate the buttons and functionality that your main bar has. The essence of a modal window is it has total control until it is dismissed.
You might try putting your UITabBarController into a NavBarController, but I'm not certain that this will work.
UITabBarController -> NavBarController -> Modal View

Objective C: How to present modal view controller from appdelegate?

I am in the appdelegate of my application. How can I add a modal view controller in the "didfinishlaunching" method?
I tried the following but did not work
SomeViewController *vc = [[SomeViewController alloc]init];
[self.tabController.navigationController presentModalViewController:vc animated:NO];
EDIT:
I changed my implementation to the following
self.tabController.selectedViewController
= [self.tabController.viewControllers objectAtIndex:0];
SomeViewController *vc = [[SomeViewController alloc]init];
[self.tabController.selectedViewController presentModalViewController:vc animated:NO];
I checked that the 'selected view controller' is not null... however I am still not able to get the output I needed. Is there anything I am missing?
Assuming tabController and navigationController are not nil, the applicationDidFinishLaunching may be too soon to display the modal view controller.
Make sure you put that code after you make the window key and visible. [self.window makeKeyAndVisible];
If that does not work try listening for the UIWindowDidBecomeKeyNotification for that window
You can try delaying presentation of that modal a few seconds using performSelector:withObject:afterDelay:

Objective-c How properly mange multiple views and controllers

I have an aplication which initially there's a TabBarController, each tab is a ViewController and every one has a button which calls other controllers.
So how am I supose to structure this? Having one main rootviewController (if so, how?)? Or calling in the appdelegate only the tabBarController and in each the viewControllers inside the tab call the other controllers?
What's the best way so I can advance, go back and transition views nimbly?
Don't know if I made myself clear...
Thanks guys.
Generally you will start with the Template called "Tab Bar Application" and as of Xcode 4 starts by loading the MainWindow Nib, which hold a tab bar and the tab bar is set up in IB to have 2 view controllers, called "FirstViewController", and "SecondViewController"...
You can follow that pattern if it suites you, otherwise you may want to start with a view based application and add your own tab bar. I personally find it to be easier to control the tab bar, through the UITabBarDelegate, especially if you plan to do anything slightly esoteric.
Edit:
Basically one of two ways, if you plan to load a Navigation controller stack, or a single modal view.
1)
ThirdViewController * controller = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
UINavigationController * myNavigationController = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentModalViewController:myNavigationController animated:YES];
[controller release];
[myNavigationController release];
2)
ThirdViewController * controller = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
[self presentModalViewController:controller animated:YES];
[controller release];
either way get back to the Tab environment by calling the following on the view controller that is calling present modal.
[self
dismissModalViewControllerAnimated:YES];