Show login view controller before tab bar controller - iphone

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.

Related

NavigationController and Modal Views

I am a newbie to iOS world and have started building custom code on top of a templated code.
So excuse me for the obvious.
The View chain starts with a MainWindow.xib which contains a App Delegate Object, a Window Object and Application ViewController. I dont understand why those objects are needed over there. But what I understand, I need to mention starting ViewController in the "Nib Name" Property to initiate my custom View Controller (called "EmptyViewController"). Its a dummy view controller, just there to avoid crash to happen as a result of missing valid viewcontroller.
I initiate a separate Modal View Controller(MainViewController) inside didFinishLaunchingWithOptions.
Code for initiating modal View Controller --
self.window.rootViewController = self.viewController;
mainView = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
// present the viewcontroller
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainView];
[self.viewController presentModalViewController:navController animated:NO];
// release it, because it's retained as modalViewController
[navController release];
I do not put this MainViewController inside MainWindow.xib as I want to have navigation at the root of MainViewController.
Inside MainViewController, I push HelpViewController when "help" button is pressed.
But HelpViewController does not show any navigation bar. I do not understand why?
Code for Pushing Navigation bar --
HelpViewController *helpVC = [[HelpViewController alloc] init];
[self.navigationController pushViewController:helpVC animated:YES];
[helpVC release];
So I would like to understand --
1) Why is MainWindow.xib needed? Can I remove it? (Note: I tried to remove it, but then I get blank screen)
1.a) Why are all the controls/objects App Delegate Object, a Window Object and Application ViewController objects needed?
2) Why doesnt HelpViewController show Navigation bar?
3) Another thing I noticed, if I say self.presentingViewController, EmptyViewController handle is returned while popViewController returns me back to MainViewController.
Thanks
The App Delegate simply implements some app-level 'callbacks' by which iOS communicates with your own code. In main.m you can see how iOS is told which of your classes implements UIApplicationDelegate. iOS creates an instance of this class and call these delegate methods ('callback') whenever appropriate (e.g. when the app goes to background).
The Window is something iOS provides, your app needs to tell what to display on it. And, as you saw, this is usually done in didFinishLaunchingWithOptions (which is called by iOS to inform your app things are ready to get started).
A View Controller is a class that handles states of stuff you show on the Window. You don't show stuff directly on the Window, but instead use Views. Every View Controller has a View with UI elements.
The XIB or NIB is a UI description/layout file. A XIB and View are linked together; you need to tell the XIB to which View Controller member (e.g. a UILabel) a UI element belongs, and you tell the XIB which View Controller method to call on a certain UI event (e.g. user taps on a button).
These are the basics. I'm aware it does not answer all your questions; I suggest you read the very good Apple documentation. Don't try to understand everything immediately as things, as you're experiencing, indeed can seem illogical at start.

display view controller before tab bar controller

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.

Xcode 4.1 Add and View TabBarController with existing ViewController

First of all sorry for my bad english and Im pretty new in these forums and Xcode programing.
So, Im writing an IPhone app with Xcode 4.1, that has Login and Register stuff visualized with UIViewController. When Im logged in, I need to visualize TabBar with different views.
I tried a lot of stuff and watched a lot of tutorials, all of them just start with the TabBarController, but I don't need it from the beginning, I just need to call it later.
The right way I believe should be just create new file .h, .m and .xib, then add the TabBarController and do a relation between TabBarController - view and File's Owner - view... but it don't let me do this thing. Obviously it don't visualize the right window.
How is the right way to do it?
Please help me, before my hair fall off...
Use the UITabBarController as the root view controller, but display a modal registration / logon view controller over the top when the app begins.
Once the user has logged in, dismiss the modal view controller to reveal the tab bar controller below.
You just use this code in your login button click or next viewcontroller viewwillappers method
UITabBarController *tabbar1 = [[UITabBarController alloc] init];
firstViewcontroller *second = [[firstViewcontroller alloc] initWithNibName:nil bundle:nil];
second.title=#"";
SecondViewController *third=[[SecondViewController alloc]initWithNibName:nil bundle:nil];
third.title=#"";
thirdViewController *one=[[thirdViewController alloc]initWithNibName:nil bundle:nil];
one.title=#"";
tabbar1.viewControllers = [NSArray arrayWithObjects:one, second,third,nil];
tabbar1.view.frame=CGRectMake(0, 0, 320, 460);
[self.view addSubview:tabbar1.view];
I am sure it will work for you i always use this code for create tab bar in any view.

Navigation based application with an add button

I created a new Navigation Based Application project.Then in MainWindow.xib I added a button to the navigationbar. I would like to push a new View onto the screen where I can enter information, which will be added as an object to the array of the UITableView.
But I don't know where to write the IBAction to link the button to (Appdelegate or the RootViewController)? Because as you see in the screenshot, it resides in MainWindow.xib because the RootViewController is merely a Table and doesn't contain the navigation. But in the document view of MainWindow.xib it is located under the RootViewController.
Do I have to create a new View Controller inside the XIB as well and create an IBOutlet for it?
I tried putting the code inside my AppDelegate and reference the button to the delegate but it doesn't work.
Please help. Thanks in advance.
See the screenshot here: http://i56.tinypic.com/5djbcm.png
When you ask yourself a question "where does this action belong" it's most probably a controller because controllers handle event flows in your app. Next question - "What controller is in charge when this action happens? What controller is most interested in this action?". Answer in your case is root table controller (RootViewController instance). Create an IBAction method in it which will push form controller (one you use to enter information) to navigation controller.
// somewhere in RootViewController.m
- (IBAction)addNewEntry {
NewEntryFormController *c = [[[NewEntryFormController alloc] init] autorelease];
// ...
[self.navigationController pushViewController:c animated:YES];
}

How do you properly set up a secondary view to support a navigation Controller on the iPhone?

I have an iPhone app that shows a simple view (View 1) that has a button. When the user presses this button, View 2 slides into view using the call
[self presentModalViewController:self.view2 animated:YES];
I want View 2 to support a navigation controller. All the code I find tells you how to set up a Navigation Controller App, but I can't figure out how to set this up using IB.
What I have done is to create a plain view2.xib file. I set the file's owner class to view2.
I add a navigation Controller to the XIB. I create an IBOutlet called view2Nav in view2.h for a UINavigationController. I link view2Nav to the NavigationController in view2.xib.
I then create a view3 class with view3.xib. I set the RootViewController in view2.xib to be of class view3 and set its NIB name to view3.
Then I go back and run the program. When I press my button on view 1, the app crashes as it tries to create view 2.
I know I must be missing a setting or something.
MySecondViewController *secondVC = [[MySecondViewController alloc] initWithNibName:#"MySecondViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondVC];
[self presentModalViewController:navigationController animated:YES];
[secondVC release];
[navigationController release];
Forget about IB. Do anything in code :) It is faster and you will exactly know why and how it works.
I'm not sure whether you can pass a self.view2 to presentModalViewController. If self.view2 is a subclass of UIViewController, you can. If it is a simple UIView, you shouldn't. If fact you can't at all.