How to place tabbar controller in second view in viewbased application? - iphone

I am very new to iPhone world. I am working on a view based project.MY first view have login page. when login is successful it moves to next view.
What i want to implement is that when i will be at second view. There will be a tabbarcontroller which have five tab items and first tab's view will be visisble. When i click other tabs, we will get next views accordingly.
Now, How to place a tab bar in second view only ?
Any kind of help will be highly appreciated.

Use [self presentModalViewController to show the login controller over your tabbar controller. After successfull login, just dismiss it.

You would need to create a ViewController which is a subclass of UITabBarViewController. Design the tabbar in nib or view lifecycle method of this controller.
After login present the new controller as [self presentModalViewController]

You need to implement your code as below.
First create a controller class for login.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self generateLoginScreen];
[self removeLoginScreen]; // On login check implement this method or u can directly write the snippet here as well.
[self prepareControllersOnTabs]; //your tab controller code function
[self.window makeKeyAndVisible];
return YES;
}
-(void) removeLoginScreen
{
[loginScreenViewController.view removeFromSuperview];
self.window.rootViewController = self.tabBarController;
[loginScreenViewController release];
}
-(void) generateLoginScreen
{
loginScreenViewController = [[LoginScreenController alloc] initWithNibName:#"LoginScreenController" bundle:[NSBundle mainBundle]];
[self.window addSubview:self.loginScreenViewController.view];
}
Hope this is exactly what u want.

Related

how to open different view controllers on app launch in iphone app?

I have two views - view1 and view2.
View1 is my default viewcontroller loaded from mainwindow.xib.
Depending on some condition checking, i want to load either View1 or View2, say if user registration is not done, load sign up screen for user, else go to default view controller.
How and where do I check this condition?
Please help.
Thanks in advance.
In your app delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(needToLogin) {
[self setViewController:[[[ViewController2 alloc] initWithNib:#"Login View"] autorelease]];
}
[window setRootViewController:viewController];
}
This will switch your view controller to the view2 view controller if needToLogin returns true. Otherwise, it will go to the default controller specified in mainwindow.xib
Another method (since you probably need the main view controller anyway) would be to present the login view controller if its needed.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(needToLogin) {
ViewController2 *loginVC = [[[ViewController2 alloc] initWithNib:#"LoginViewController"] autorelease];
[[self viewController] presentViewController:loginVC animated:NO];
}
[window setRootViewController:viewController];
}
Note, you will have to call [self dismissViewControllerAnimated:YES] to get rid of the login view.
Edit: Respones from OP:
I tried first one,
if(loginflag){
[self setViewController:[[[SignUpViewController alloc] initWithNibName:#"SignUpViewController" bundle:nil] autorelease]];
}
[self.window setRootViewController:self.signUpView];
Try this instead:
if(loginFlag) {
[self setViewController:[[[SignUpViewController alloc] initWithNibName:#"SignUpViewController" bundle:nil] autorelease]];
}
[[self window] setRootViewController:[self viewController]];
If you are just planning on bringing up a sign-up screen if the user registration is needed, why not stick with the default view controller, but at -applicationDidBecomeActive: present a modal view controller for the sign up view?
With the information given, you could just create a flag in the AppDelegate that can be persisted based on the user registration is complete or not. Then in the "didFinishLaunching..." method you could check this flag and load the the proper view based on this.

Creating a navigation controller that doesn't show up on startup

I want to do this simple thing: create a uinavigationcontroller but that doesnt show up on launch. Let's say I want to have a welcome screen with a "Go" button that leads to the uinavigationcontroller. In all the examples that I have seen it looks like the navigationcontroller appears right away. How should I go about doing that?
Thanks!
If by "show up" you mean using a navigation controller with no visible footprint, that's easy. Simply do this in the root view controller:
// Root
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
For the child view controllers, you need to do something similar in order to display the navigation bar when they appear.
// Child
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
This is a pretty common technique for apps with main menus where you don't want to display a navigation bar in the main menu view.
The mecanic of displaying the navigation controller's view takes place in the -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; method, in the app delegate. There the NC is added as the rootViewController of the window.
If you want to display another one, just set your custom view controller right in place of the NC and then switch the two view controllers (replace the first custom view controller with the NC) in the action method called when the button is pressed.
Assuming myCustomController defines a UIButton property called touchButton :
// in the app delegate
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//self.window.rootViewController = self.navigationController;
[self.myCustomController.touchButton addTarget:self
action:#selector(switchVC)
forControlEvents:UIControlEventTouchUpInside];
self.window.rootViewController = self.myCustomController;
[self.window makeKeyAndVisible];
return YES;
}
Now write in your app delegate an action method :
-(void)switchVC {
self.window.rootViewController = self.navigationController;
}
in viewcontroller that shows go hide navigation bar and in other show navigation bar
[self.navigationController setNavigationBarHidden:YES];
hope this will help

Help with Modal View Controller & Login

I need some help with modal view controllers, as I have not used Modal View Controllers before...
So this is how my application now is...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[_window addSubview:rootController.view];
rootController.selectedIndex =2;
NSLog(#"Displaying the APP delegate");
[self.window makeKeyAndVisible];
return YES;
}
I have a view for Login, titled LoginViewController . I want this to appear first as the first view, and on clicking the Login button (IBAction), I want it to show rootController.selectedIndex =2;
(Please ignore the login check as of now). I just want the login view controller to appear at first, and dismiss itself if I press Login, and then take the screen to my rootController (which is a UITabBarController)
In the viewDidLoad method of the first view in the tabBar present the modal view as follows :
LoginViewController *lvc = [[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:lvc animated:YES];
[lvc release];
To dismiss the modalView, in the IBAction of the login button just put in the following code :
[self dismissModalViewControllerAnimated:YES];

How to add navigationController to existing tab application?

I have an iPhone application with 2 tabs. This is my application delegate:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
My first tab is a subclass of UITableViewController. I would like to push a new view controller when someone clicks on a row of my table. I know that this is done in didSelectRowAtIndexPath like this:
TableViewDetailViewController *fvController = [[TableViewDetailViewController alloc] initWithNibName:#"TableViewDetailViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:fvController animated:YES];
[fvController release];
fvController = nil;
My self.navigationController is NIL so nothing gets pushed to the view stack. Where should I create my navigationController, so there would be as little code to change as possible?
We have direct solution using Interface builder.
In interface builder go to xib in which you have added tabBarController and delete the tabBarItem with which you have connected your tableViewController .
Now, drag and drop an UINavigationController from library in your tabBar and set this navigationController's rootViewController as your tableViewController.
Thanks
Instead of setting your TableViewDetailViewController as one of your tabbbars ViewControllers, you should create an UINavigationController for each tab on your tabbar. Then place your TableViewDetailViewController inside the corresponding UINavigationController. self.navigationController will point to a valid UINavigationController in that case.

modalViewController present when app opens

Hey im trying to display a modal view controller as soon as my tab bar controller app opens.
There is something wrong with the code below, and im 99% sure its the code for this. what do i put for the thing im calling it on?
[self presentModalViewController:promt animated:YES];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch.
//Displays the password prompt modally
PasswordPromViewController *promt = [[PasswordPromViewController alloc] initWithNibName:#"PasswordPromViewController" bundle:nil];
promt.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:promt animated:YES];
[promt release];
return YES;
}
any ideas would be helful!
Cheers
I'm guessing you're adding this code in the application delegate file (eg if your app is called XXX then XXXAppDelegate.m). If this is the case you cannot use:
[self presentModalViewController:promt animated:YES];
as this method has to be called on an instance of a UIViewController. If you've set up your project in the standard way then your app delegate should have an object called window, which is a reference to the main window of the application. It's probably simplest if you add the modal view controller to that, like so:
[window presentModalViewController:promt animated:YES];