display view controller before tab bar controller - iphone

I'm starting my first application for iphone. I'm using xcode 4.3.3, IOS 5, and the principle of storyboard.
the home screen of the application is a tab bar controller and I want to display a login before the home screen if the user does not logged.
I can not find a solution: if I have to use the file AppDelegate.m with the function didFinishLaunchingWithOptions() or file of my controller with the function viewDidAppear() or something else.
if someone would help me for a solution
Thank you.

just create the login screen when your app is launched and when your login is succeed push your tab bar controller from there...

It is better to add function in AppDelegate.m to remove unwanted window appearing if not logged in (Your home view will be shown for a moment before redirecting to login page if you write code in ViewDidAppear method).
Another method is add a new view controller and check where to redirect based on log in status from view controller's ViewDidAppear method.

Try using a Modal View Controller, Docs
On didFinishLaunchingWithOptions() or viewWillAppear() try do something like this:
YourViewController *viewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
viewController.modalPresentationStyle = UIModalPresentationFormSheet;
//Present as Model view controller
["presentedViewController" presentModalViewController:viewController animated:YES];
//release it After presenting to it
[viewController release];
Then to remove it call: dismissModalViewControllerAnimated: (docs)

you can use another view with login screen and Save Bool value in nsuserdeafault then when app is start check for nsuserdefault and show view according to that.
then after you can call everywhere where you want in delegate.m or viewwillappear.

Related

How to develop a TabBar based application with a login functionality?

I am developing an application where i need to show a list as a menu(Courses,lessons,grade,logout) to the user. so even before this i need to show a login screen. Only upon successful and valid login i need to re-direct the user to the menu. So i have planned to develop a tabBar based application with 4 tabs. Here i am confused on how to add the login view controller even before the TabBar controller is loaded. I want the first tab to be selected every time. As of now i am adding my TabBar controller as a rootviewcontroller to my AppDelegate window and then presenting the login view controller as a modal view controller. But the problem here is even before the Login View controller is loaded, my courses view controller is loaded because the tabbarcontroller is loaded first. My actual requirement is i need to load the course view controller with the list of courses based on the inputs given in the Login View controller. But loadview of course view controller is loaded even before the load view of login view controller. so my list of courses is always the same irrespective of who logs in. I am confused here on how to move forward...Any suggestion here would be of great help...
So, a very quick example, could be; in your loginViewController you should have some method something like this:
//Call this after the user has done with the login
-(IBAction)remove:(id)sender{
AppDelegate *del=(AppDelegate*)[[UIApplication sharedApplication] delegate];
//Set some data based on the user's input (eg some property shared in the AppDelegate)
//del.dataEnterByTheUser=someData;
[del removeLoginView];
}
Then in your AppDelegate (assuming that now the rootViewController is the loginViewController) you could do like this (you can optimize the transition):
-(void)removeLoginView{
UITabBarController *tabVC=[[UITabBarController alloc] init];
ViewController *v1=[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
//v1.data=self.dataEnterByTheUser;
ViewController *v2=[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
NSArray *arrayVC=[NSArray arrayWithObjects:v1,v2, nil];
[tabVC setViewControllers:arrayVC];
[tabVC setSelectedViewController:0];
CGRect rectVC=self.loginViewController.view.frame;
rectVC.origin.y=self.view.frame.size.height;
[UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.loginViewController.view.frame=rectVC;
} completion:^(BOOL finished){
[self.loginViewController.view removeFromSuperview];
self.loginViewController=nil;
self.window.rootViewController=tabVC;
}];
}
Also remember to set in each viewControllers's initWithNibName: the self.title to set the title on the tabItem.
No need to fiddle around with the rootViewController...
Just add the following code at the beginning of your viewWillAppear: method of the view controller which would normally appear first (most likely the VC you are presenting in the first tab):
[self.tabBarController presentModalViewController:loginController animated:NO];
Where loginController is obviously the view controller which manages your login screen. If you show it without animation, it will be the first thing visible when your app launches (after the default image disappears). I've used the same method to show a disclaimer page which the user must read before using the app. It's working just fine and made it to the store without problems.
Edit: In this solution, the loginController must dismiss itself once the user has successfully logged in:
[self dismissModalViewControllerAnimated:NO]; //Although you might do this animated, this time
You can just change the array of view controllers in the tab bar controller at runtime. That should be sufficient for your purposes.
I've written a small example. Try to login with the following credentials:
username: john, password: doe
username: pete, password: poe
You will see a different combination of tabs depending on the login used.
The example can be downloaded from my Dropbox: http://dl.dropbox.com/u/6487838/LoginTabExample.zip

Show login view controller before tab bar controller

I am new to iphone development. I am developing an iphone application which contains four tabs. I have implemented it using tab bar controller. But now i need to show a login screen without tabs before tab bar controller. I have tried so many methods but didnt get the one i wanted.
Can anyone pls explain how to do this with a code snippet??
Create a new class LoginViewController. When your application launches then add the view to the window. Now when login is successful then remove it from superview and add the MainController.
Create a subclass of UITabBarController (though it's not advised by apple), but for this purposes it should be ok and do this in viewWillAppear
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
BOOL isLogged in = //do something to determine if you're logged in
if(!loggedIn){
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewControllerNibHere" bundle:nil];
[self presentModalViewController:loginViewController animated:YES]; //or NO if you don't want it animated
[loginViewController release];
}
}
Or add this to a category for UITabBarController and import it in the app delegate or wherever you're using the UITabBarController
Create a new UIViewController subclass with a nib that represents your login screen (I'll refer to it as SignInViewController).
Open your MainWindow.nib file and add a new UIViewController
Set the new UIViewController's class type to SignInViewController
Set the UIWindow's rootViewController outlet to the new SignInViewController
Now create a new nib file and copy your existing UITabBarController to it (it's best to split nibs rather than having a single-mega nib)
Back in your MainWindow.xib change the existing UITabBarController's attributes to specify the nib name that you just created
Check this Link's source code,
it uses Login Controller as modal view with 4 tabs
http://code.google.com/p/tweetero/source/checkout
Also i tried this way ,
in my first tab view - in viewDidAppear - i'll check Login = YES then
show the LoginController
- [self.tabbarcontroller presentMOdalViewcontroller:LoginView animated:YES];
so every time u click on first tab - if u need to Login put a flag - check it & show Login View
Hope this Helps.
The best way to do this is to create a new LoginViewController as other people have mentioned and then set your rootviewController to tabBarcontroller as soon as you authenticate the user. Here is how you can do this in swift, this is snippet to put as soon you authenticate your user in LoginViewController
let tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = tabBarController
Where TabBarController is the storyboard id of your tab bar controller. It could be anything whatever name you have given it.

not getting tab view when we press back

hii every one
i have created a test project with tab view controller, on click of a tab it will goto that coresponding screen(say screen A) when i click back button in screen A it will come back to main page but with out tabView,
following is may code for back button where DataEntry is a class name to where i am navigating
DataEntry *avController;
UINavigationController *addNavigationController;
if(avController == nil)
avController = [[DataEntry alloc] initWithNibName:nil bundle:nil];
if(addNavigationController == nil)
addNavigationController = [[UINavigationController alloc] initWithRootViewController:avController];
//avController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentModalViewController:addNavigationController animated:YES];
insted of the above code if i use following code
[self.navigationController dismissModalViewControllerAnimated:YES];
it will work fine & ill get back to main page with tabs , but it wont run the updated code which is in the viewDidLoad so i need to navigate to the main page insted of using dismissModalViewController
can any one tell me how can i get tabView when i navigate to the main page with out using dismissModalViewController
this is not correct:
[self.navigationController presentModalViewController:addNavigationController animated:YES];
(this is not what you mean to have: it will present a modal view, when you dismiss it, it will uncover what was below, i.e. your tab)
you should use pushViewController to show your avController so that the back button is activated and so on.
look at this.
I'm struggling a little to understand your problem, but viewDidLoad only runs when the view loads: not when you come back to screen A : its still loaded then. To run code each time screen A is displayed, take a look at viewWillAppear / viewDidAppear http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
I tend to create tabs programatically rather than using the IB: its clearer whats going on. Take a look at this answer to show how I do it.
UITabBar with UINavigationController in code

iphone - Modal view controller disappears?

So let's say I have a viewController named homeViewController, and another view controller named listViewController
I display listViewController on top of homeViewController as a modal.
If the user clicks the off button, and then comes back to the app the modalViewController is gone.
ListViewController *listViewController = [[ListViewController alloc] init];
[self presentModalViewController:listViewController animated:NO];
[listViewController release];
Note: Application doesn't startup from scratch when this occures and the previous state is still visible
I'm assuming that by "off button" you mean the user locks the iDevice.
I just tried this in one of my apps and the modal view controller is still there after unlocking. My guess would be that it's something unrelated to the code you have posted. I would check your - (void)applicationWillResignActive:(UIApplication *)application method in your app delegate class and see if there's anything there that would dismiss the modal view controller.
Here is what the problem was.
When the user locks the screen I remove homeViewController from window
[homeViewController removeFromSuperview];
When user starts the app again I do
[windows addSubview:homeViewController];
that brings homeViewController on top of its modeal

Showing a modal view controller from a tab bar app

First, I would like to warn that I am a complete newbie into iPhone coding...
I need to show up a viewcontroller from a library, I know that it is modal. I have a tab bar app (created with the default XCode template). I need to show that viewcontroller, there are no problem if it hides the tabbar itself... But I am quite clueless, I don't know even what to search, or what to read...
You can call presentModalViewController:animated: to display another UIViewController modally.
EDIT: If you want to display your modal view in response to a button touch (for example), you would display it like this:
- (IBAction)buttonTouched:(id)sender
{
ModalViewController* controller = [[ModalViewController alloc] init];
[self presentModalViewController:controller animated:YES];
[controller release];
}
Then when you want to dismiss the modal controller, call dismissModalViewControllerAnimated:. This can be called either on your main view controller, or the modal one.
I don't know even what to search, or
what to read...
View Controller Programming Guide is a good place to start to help you understand view controllers (including modal ones). If that's confusing, get a bigger picture with iOS Application Programming Guide or start at the very beginning.
You can call modal view as
YourViewController *yvc = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:YES]
[self presentModalViewController:yvc animated:YES];
You can call it in the IBAction method in case you want to call it on any control event like Button Click
-(IBAction)buttonClicked:(id)sender
{
YourViewController *yvc = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:YES]
[self presentModalViewController:yvc animated:YES];
}
You can call it using self.
Hope this helps you.
If you have more doubts on this then you can ask me.