I have two UINavigationControllers like this:
LoginNavigationController -> LoginView
HomeNavigationController -> HomeView -> ...
From LoginView I navigate to HomeView modally like this:
[self presentViewController:HomeNavigationController animated:YES completion:nil];
When the app goes to background I use an observer in LoginView that dismisses HomeNavigationController.
There is a case though, in which I must present HomeNavigationController directly, without asking the user to login. I do this from another storyboard and reset my root view controller:
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName:kStoryMain bundle:nil];
UINavigationController *homeViewController = (UINavigationController *)[secondStoryboard instantiateViewControllerWithIdentifier:kControllerHomeNavigation];
appDelegate.window.rootViewController = homeViewController;
[appDelegate.window makeKeyAndVisible];
But when the app goes to background, I still must redirect to LoginView.
Is there a way to redirect to LoginNavigationController, add the observer and present modally the HomeNavigationController, and all this to happen seamlessly for the users, without them noticing that Login is being created?
If not, should I somehow change my approach to the task?
You can use this approach to get the required results seamlessly,
1) Make HomeNavigationController as the root view controller of your app.
2) In the first view of HomeNavigationController say it HomeViewController, override viewWillAppear and check if you need to show login view ( You may check this by putting any flag to true when user enters to background or something).
3) If you need to show the LoginNavigationController,show it as modal without animation like,
[self presentViewController: LoginNavigationController animated:NO completion:nil];
Please let me know if you find something confusing.
Related
Here is the scenario:
The first scene in my storyboard is a login view. It's a UIViewController. When the user is logged in, it shows the home view which is embedded in a navigation controller. I'm adding a log out functionality which should take me back to the first scene in the storyboard which is the login view. How do I do that?
Here is an image of the storyboard showing the login view -> navigation controller -> home view
This is my implementation so far. In the log out action, I clear the session, and pop to root view controller. It does not work because I am still stuck on the home view since it is the root view controller of the navigation controller. However, If I restart the app, the user is logged out and I'm left with the login view.
Code:
[self.navigationController popToRootViewControllerAnimated:NO];
// Set at beginning of storyboard
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
app.loginViewController = [mystoryboard instantiateViewControllerWithIdentifier:#"loginViewController"];
Use unwind segues for that.
In your LoginViewController, declare a method with this signature
- (IBAction)unwindToLoginViewController:(UIStoryboardSegue*)segue
Go to your HomeViewController and control drag from your logout button to the Exit button at the top of your view controller window (see screenshot below), then select the unwindToLoginViewController segue. That's it!
U can pop by using navigationController.viewControllers.Get all View Controllers among navigationController,identify it and then pop.If u have pushed the segue from LoginView to HomeView
if([self.navigationController.viewControllers[0] isKindOfClass:[LoginViewController class]])
{
[self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES];
}
Hope it helps you...
Try this answer. First you create a navigation controller. make it "is initial View Controller". After that connect login Viewcontroller as a root view controller And connect home controller with facebook button Action.
Navigation Controller -> Login Controller -> Home Controller
Your Storyboard is look like this
After that when you logout from HomeViewController then Just add this method:
-(IBAction)logOut_Action:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
Its working Fine. Please implement like this and let me know if you face any problem. :)
Try this:
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
The following scenario:
My iPad app has a SplitViewController as it's main VC. After starting the app (new or from background) I have a fullscreen login view that (obviously) disappears after entering the correct password.
The problem:
After login, I want to present the exact same screen that was there BEFORE moving to background. This works fine UNLESS there is a modal view on top of the split view (like settings etc).
What I tried:
In AppDelegate I store my self.window.rootViewController, make the login vc as my root vc and after login I set my stored root VC as actual root VC. But then the (modal) settings view is not visible and can not be opened again (Warning: Attempt to present VC on SplitVC which is already presenting VC). In fact, no other modal view can ever be opened (unless app is properly closed).
Second try: Instead of setting the login VC as root VC I presented it as a fullscreen modal view on top of my split view. This yielded the same error message as the first try but a different result. After entering background mode the login VC won't be presented at all (since there already was a modal view).
This is b'coz you R trying to present the VC while it is actually loaded as RootViewController .
Try using this :
UISplitViewController :
Once loaded the Root and as well as MasterViewControlller , You will make the UIViewControllers as SubViews for Your
RootViewController.. From the UIViewController , If you want to revert
back to the RootViewController , Try this :
[self.navigationController popToRootViewControllerAnimated:NO];
I just thought your problem is similar.
Hope it helps.
What I did was the following:
Create a property that can store my modally presented VCs (they are all embedded in a UINavigationController)
#property (nonatomic) UINavigationController *navController;
When creating the login vc I store my modal vc (which may be nil which is fine), dismiss it and present the login vc
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
LoginViewController *loginViewController = [storyboard instantiateViewControllerWithIdentifier:#"LoginView"];
self.navController = (UINavigationController *)self.window.rootViewController.presentedViewController;
if (self.navController) {
[self.navController dismissViewControllerAnimated:NO completion:nil];
}
[self.window.rootViewController presentViewController:loginViewController animated:NO completion:nil];
And when the login is successfull I dismiss the login vc and restore the modal vc (if available)
if (self.navController) {
[self.window.rootViewController presentViewController:self.navController animated:NO completion:nil];
}
Can you try this way.
Root VC is main screen not login page.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Inside the above code, present your login page to your root VC.
- (void)applicationDidBecomeActive:(UIApplication *)application{
//Use below method or similar method to remove any presented VC on Root VC
if ([((UINavigationController *)self.window.rootViewController).visibleViewController isKindOfClass:[RLSplashViewController class]]) {
NSLog(#"AppDelegate dismiss splash page");
[self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
}
//And present your login VC
}
Inside the above code, First and very import, remove any VC that presented on the root VC. And then present login page to your root VC.
I am currently using this way to present splash page(from background or new), in your case, it is login page. Hopes this is helpful for your case : )
I have an app with a LoginViewController as the initial view.
Note: So in appDelegate.m, self.window.rootViewController is NOT the TabBarController.
After Auth, I present the main part of the app, which has a tabbarController (identifier:tabBar) with two tabs and one tab has a navigation controller. I am using Core Data, so I need to pass MOC.
If I use,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UITabBarController *obj=[storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
[obj setSelectedIndex:0];// Which tab to show first
[self presentModalViewController:obj animated:YES];
It works good visually. Now I need to pass the MOC. Read about PrepareToSegue method,created a segue (modal, Not shown in pic) from loginVC to my TargetViewController (TabBar>NavigationController1>View1), named the segue "LoginSegue" and used the following code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController *navController = (UINavigationController *)[segue destinationViewController];
View1 *devicelist = (View1 *)[[navController viewControllers] lastObject];
devicelist.managedObjectContext = managedObjectContext;
}
The TabBar does not show. How do i set the tabbar controller in this case?
I have been trying to get a grasp on getting a reference for the Modal Tabbar, but still not clear. Can some one explain in layman terms how to handle a situation like this?
I think it would be better to use a design that doesn't use a modal transition to the tab bar controller. Modal presentations are generally supposed to be for interruptions to the normal flow of the app, not for getting your main controller on the screen. There are two alternatives, that I think are better. You can leave the login controller as the initial root view controller of the window, but then switch it out for the tab bar controller (which will be the new root view controller of the window, and the login controller will be deallocated). This usually works ok, but I think in this case where you want to pass the MOC from the app delegate (I presume) to a controller in the tab bar controller, I think a second way would be better.
The second way to do this, and the way I usually do login controllers, is to have the tab bar controller be the root view controller of the window, and then present the login controller modally from the viewDidAppear method of the initial view (which would be the one you're calling View1). If you do this presentation with animation set to NO, the login controller will be the first thing the user sees:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
static int first = 1;
if (first) {
LoginViewController *login = [self.storyboard instantiateViewControllerWithIdentifier:#"Login"];
[self presentViewController:login animated:NO completion:nil];
first = 0;
}
}
The if statement is in there so the presentation doesn't happen again when you come back from the login controller (you could do something more sophisticated like having a delegate call back to View1 from the login controller indicating that the login was successful if you want, but this works).
If the login succeeds, you just dismiss the login controller, and you'll be there in your first view (if it fails, you just never dismiss it, and maybe put up a message saying the login failed).
If you go this route, then you can pass the MOC in the app delegate like this:
UINavigationController *nav = [(UITabBarController *)self.window.rootViewController viewControllers][0];
View1 *devicelist = (View1 *)nav.viewControllers.lastObject;
devicelist.managedObjectContext = managedObjectContext;
I want to know if it is possible to have a first view that acts as a menu (that has a couple of buttons for which one is Edit). I want to be able when I click on edit to then show a split view. Now, when I do that, I get the error :
Application tried to present a Split View Controllers modally
My code in the action method of the edit button is :
UIStoryboard *editorStoryboard = [UIStoryboard storyboardWithName:#"EditorStoryboard" bundle:nil];
UIViewController *editorViewController = [editorStoryboard instantiateInitialViewController];
editorViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:editorViewController animated:YES completion:nil];
So I am assuming, the ViewController that contains the button is the root VC. You should change that with the SplitVC when tapping the button (add a nice animation), because the split should be the root of your app (it should not be presented modally). Hope this helps!
Edit:
It should look something like:
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.window.rootViewContrller = mySplitViewController;
You can also do this in your custom segue if you have one.
I am working on an app that at launch checks for valid login credentials, and if they are found and not expired the main split view controller is displayed, and if not a login screen should be displayed.
Each part is working fine separately, but I am struggling with the best way at launch time to select the proper view to display.
I have tried setting up a modal segue from the root view controller, and in my application:didFinishLaunchingWithOptions: function in the App Delegate, calling this:
// Segue to the login view controller...
if (loginNeeded) {
[self.window.rootViewController performSegueWithIdentifier:#"LoginScreen" sender:self];
}
This logically should work, but triggering segues from within the app delegate seems to be impossible.
What is the ideal place and technique for handling this?
You could try a custom segue, as per this post hiding-a-segue-on-login-process.
Alternatively if you're desperate to have the login display before the split view controller loads try something along the following lines...
Create your login screen on the main storyboard as, say, a subclass of UIViewController. Make sure it is the initial scene (check Is Initial View Controller).
On the storyboard, create a new segue from your login class to the original SplitViewController. Give it an identifier, 'Load SplitViewController' and a segue custom class name which we'll call FullyReplaceSegue.
In your login class .m file, add code to be called once the user has logged in:
[self performSegueWithIdentifier:#"Load SplitViewController" sender:self];
Create the new segue class, based on UIStoryboardSegue and name it FullyReplaceSegue as per above.
.h file
#import <UIKit/UIKit.h>
#interface : UIStoryboardSegue
#end
.m file
#import "FullyReplaceSegue.h"
#implementation FullyReplaceSegue
- (void)perform
{
UIViewController *dest = (UIViewController *) super.destinationViewController;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
window.rootViewController = dest;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UISplitViewController *splitViewController = (UISplitViewController *)dest; // assumes we're transitioning to a UISplitViewController!
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
}
}
#end
Here's how I did it.
In didFinishLaunchingWithOptions:
//save the root view controller
[[self window] makeKeyAndVisible];
UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController;
rootController = [[navigationController viewControllers] objectAtIndex:0];
Somewhere else in the app delegate:
[rootController performSegueWithIdentifier:#"fileSegueID" sender:self];
Then, in the storyboard, create a segue from the view that gets assigned as "rootController", to the desired optional view, and give that new segue the id fileSegueID. It takes some debugging to make sure the rootController variable gets assigned to the correct view.
Maybe a little late, but I was looking for the same suggestions. Here's what I wound up doing.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Signup" bundle:nil];
if(isLoggedIn) {
UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController;
IndexController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"IndexController"];
[navigationController pushViewController:ivc animated:NO];
}
Why don't you load the screen that would be visible assuming proper and non-expired log-in credentials (by setting it as the root view controller of the window), and then in viewDidLoad of that first view controller, check if an update to the login credentials are needed. If so, segue into the login view controller.
Yes, it can be used, if you get a reference to the segue's parent view controller. You can get it like this:
UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController;
[[[navigationController viewControllers] objectAtIndex:0] performSegueWithIdentifier:#"LoginScreen" sender:self];
This will only work if the index in viewControllers array matches the one of your view controller and if it exists of course. In this case is the first one (in the array and storyboard).
The segue ("LoginScreen") must not be attached to an action. The way you do this is by control-dragging from the file owner icon at the bottom of the storyboard scene to the destination scene. A popup will appear that will ask for an option in “Manual Segue”; pick “Push” as the type. Tap on the little square and make sure you’re in the Attributes Inspector. Give it an identifier which you will use to refer to it in code.