viewController initial class set - ios

I have two view controllers, a default ViewController and the one I added later for login: LoginViewController. I set LoginViewController as initial view. But when I run the app, after viewDidLoad in LoginViewController it fires -viewDidLoad in ViewController also.
I have a modal segue, after login is successful to perform segue and show me the ViewController, that works. But in execution of app ViewController is also fired. Does anyone have an idea why this happens? AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"LoginView"];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}

Related

iOS MMDrawerController objective-c login view controller

I am begin to study iOS and I try to do left navigation with MMDrawerController
my AppDelegate didFinishLaunchingWithOptions code is:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *leftView = [mainStoryboard instantiateViewControllerWithIdentifier:#"LeftViewController"];
UINavigationController *leftNav = [[UINavigationController alloc]initWithRootViewController:leftView];
UIViewController *centerView = [mainStoryboard instantiateViewControllerWithIdentifier:#"CenterViewController"];
UINavigationController *centerNav = [[UINavigationController alloc]initWithRootViewController:centerView ];
self.drawerController = [[MMDrawerController alloc] initWithCenterViewController:centerNav leftDrawerViewController:leftNav];
self.drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
self.drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModePanningCenterView;
self.window.rootViewController = self.drawerController;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
So it's work fine, but I have LoginViewController on my app, and if user has no saved token on NSUserDefaults, I must show LogionViewController.
Of course side menu must be hidden on LoginViewController.
I Tried to switch to LoginViewController inside my CenterViewController:
- (void)viewDidLoad {
[super viewDidLoad];
LoginViewController * vc = [[LoginViewController alloc] init];
AppDelegate *app = [[UIApplication sharedApplication] delegate];
[app.drawerController setCenterViewController:vc withCloseAnimation:YES completion:nil];
}
But I have black screen only.
What I do wrong?
Thanks
What you're doing is a bit weird because you are setting the new centerViewController (of type LoginViewController) within the current one (of type CenterViewController), and once that is done the latter one will be deallocated because there are no more references to it. This might somehow be causing the black screen.
One solution would be to have the LoginViewController outside the MMDrawerController, and always present it at the beginning. If there is no token, then quickly (without animation) present the MMDrawerController and the LoginViewController won't even be seen. This way also allows you to easily dismiss back to the login screen if the user logs out.
Another option is to just present your LoginViewController from the CenterViewController modally (or however you like really) using presentViewController:animated:completion:, and then just dismiss it when they log in.

Skip Login View Controller in the NavigationController

I have a tab bar which contains five tabs. My application does not required user to be logged in. Only some features will be allowed if a user signups or logins.
If a user registered/logged in already, I implemented the following logic in the UserViewController which is connected to the NavigationController. However, in the following logic, user still could able to see that ViewController in one-two seconds.
-(void) viewWillAppear: (BOOL) animated
{
if(isRegistered)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UserProfileViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"UserProfileVewController"];
[self.navigationController pushViewController:vc animated:YES];
}
}
I wonder if a user registered already, how could I skip UserViewController?
I want tabbar clicks to open directly to the UserProfileVewController rather than UserViewController which is login/signup viewcontroller.
In the project's AppDelegate, you could check if user is logged/registered or not, and display the ViewControllers dependently:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
BOOL userIsLoggedIn = AMethodCheckIfUserLoggedIn();
if (userIsLoggedIn) {
UserProfileViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"UserProfileVewController"];
self.window.rootViewController = vc;
}
else{
UserViewController *userViewController = [storyboard instantiateViewControllerWithIdentifier:#"UserViewController"];
self.window.rootViewController = userViewController;
}
//... Other logic goes here
[self.window makeKeyAndVisible];
return YES;
}
You could try having your app delegate (or whomever you want really) conform to the UITabBarControllerDelegate and implement - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController. In there you might be able to do the necessary checks/fiddling of the view controllers maybe.

How to set the view controller as root view controller in AppDelegate

I have one view controller with out navigation controller. Its name is LoginViewController. In my AppDelegate, I want to keep my LoginViewController as root view controller.
How can I do this in Objective-C? How can I set my view controller as root view controller?
Note: My view controller does not have navigation view controller. It's a single view controller.
do like
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1. get the Storyboard Name
UIStoryboard* main = [UIStoryboard storyboardWithName:#"Main"
bundle:[NSBundle mainBundle]];
//2. get the ViewController using Storyboard ID
UIViewController *viewConr = [main instantiateViewControllerWithIdentifier:#"HomeViewController"];
// 3.finally assign the Root
self.window.rootViewController = viewConr;
[self.window makeKeyAndVisible];
return YES;
}
for E.g
Without storyboard:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
LoginViewcontroller *Vc = [[LoginViewcontroller alloc]init];
self.window.rootViewController = Vc;
[self.window makeKeyAndVisible];
return YES;
}
If using storyboard just make that view controller as initial view controller from storyboard.
If you want to set home page VC as root vc from login page VC
Import appdelegate in login page
#import "AppDelegate.h"//Import in Login page VC
self.delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; // In viewDidLoad
Write below code where you navigate
//Make root view controller
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
HomeViewController * hvc = [mainStoryBoard instantiateViewControllerWithIdentifier:#"Home"];//Your Home page story board ID
self.delegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:hvc];
[self.delegate.window makeKeyAndVisible];

Changing the first ViewController in iOS

Initially I developed the app with only one ViewController(called MainView). Now I'd like to add one ViewController (called LoginView)infront of MainView. I added in the AppDelegate as
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LoginPage *RootViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:#"LoginPage"];
[UIApplication sharedApplication].delegate.window.rootViewController = RootViewController;
[[UIApplication sharedApplication].delegate.window makeKeyAndVisible];
return YES;
}
It looks working, but only black screen appears.
My LoginPage ViewController has the following structure. The ViewController has two buttons, one label and one text view. They don't appear and only black screen is appearer.
Just mark your Login view controller as initial view controller in storyboard

How to programmatically set which screen load first by story board

I am working on a app which have login screen. It records whether user logged in or not, in user defaults. I wanted to direct user to the login page if he has not logged in otherwise navigate to the apps main screen. How to do this programmatically using story board.
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"loggedin"]) {
//If logged in
} else {
//if logged out
}
In your appdelegate write this code. And dont forget to give identifer name to your viewcontrollers
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
Why don't you always start your app with a "Loading" view. The LoadingViewController checks if the user is logged in or not and then transitions to the proper viewcontroller.

Resources