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

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.

Related

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

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.

Modal LoginView with iPhone

I have a viewcontroller containing a TabController. Before this loads, I want a user to login so I can check what they have access to. In my AppDelegate, bot the rootViewController (with the tabs) and the LoginViewController are declared, and they're also wired up in IB:
I have this in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// [window addSubview:[rootController view]];
[window addSubview:[loginViewController view]];
[self.window makeKeyAndVisible];
return YES;
}
My plan was to dismiss the login form after authenticating and show the rootController, but the rootController displays straight away. I was going to do:
-(IBAction)DidClickLoginButton:(id)sender {
NotesAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.window addSubview:[delegate.rootController view]];
[self dismissModalViewControllerAnimated:YES];
}
Is there an easier way to do this? I can't see why the LoginViewController isn't presented.
EDIT: Eventually got this working by adding it to the rootController in my AppDelegate's didFinishLaunchingWithOptions method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// [window addSubview:[rootController view]];
[self.window makeKeyAndVisible];
LoginViewController *loginViewController =[[LoginViewController alloc] initWithNibName:#"LoginView" bundle:nil];
[self.rootController presentModalViewController:loginViewController animated:YES];
return YES;
}
I think it's actually much easier to do things 100% programmatically rather than with Interface Builder. Either way, in application:didFinishLaunchingWithOptions:, you want to do something like this:
[rootViewController presentModalViewController:loginViewController animated:NO];
Then, after the user logs in, do:
[rootViewController dismissModalViewControllerAnimated:YES];
Yes it would. As you are adding the root controller view on the window, and this would make it appear above all (even above the login view) and then your login view gets dismissed behind the root view, which you cannot see.
EDIT:
One of the approach would be to have login controller above root in the beginning itself (root view controller presenting login view) and then happily dismiss the login view.

load .xib file and display it

I'm trying to display an "About Page" in my application when pressing a button, but when I press the UIButton, the NSLog(#"Button pressed") is called, but the view won't load. Here is my code:
- (IBAction)pushAbout {
NSLog(#"Button pressed");
AboutView * aboutView = [[AboutView alloc] initWithNibName:#"AboutView" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[aboutView release];
}
I've seen this code in an example, but it was called in a UITableViewController instead of an ordianry UIView controller.
The class files "AboutView" were created as UIViewController subclass. They were created and left untouched
A guess: Your view is not in a UINavigationController, hence
self.navigationController
is actually nil, and nothing happens.
The solution would be to place the main view in a nav controller, and adding the nav controller's view to your application's main window.
A simple way of doing this:
In your app delegate, change
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController* navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
Or similar. Note that this is just to get you started, and maybe not the nicest way of doing it. (I assumed you've just created a view-based application in xcode.)
Yes you don't have a UINavigationController set.
Add
NSLog(#"nav? %#", self.navigationController);
to your action and you'll see that it dumps (null).
However, the AboutView.xib works fine if you enter this code:
[self presentModalViewController:aboutView animated:YES];
instead of
[self.navigationController pushViewController:aboutView animated:YES];
The view will show up. In you zipped example the AboutView.xib didn't contain a label, so don't wonder if it turns out to be a white page.
You can dismiss the presented modal view by using
[self dismissModalViewControllerAnimated:YES];
in your AboutViewController.
To get your hands on a UINavigationController I suggest creating an app with the Navigation-Based Application template.

DismissmodalViewController donot remove the modal View

I have been figuring out this since yesterday but have not got that correct yet.
I have added the modalviewcontroller for my loading view controller on top of my tab bar controller and it works fine.
Added the code in app Delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
[navController.navigationBar setTintColor:[UIColor blackColor]];
[window addSubview:rootController.view];
[window makeKeyAndVisible];
LoadingViewController *lvc = [[LoadingViewController alloc] initWithNibName:#"LoadingView" bundle:nil];
// Delegate added here
lvc.loadingDelegate = self;
[rootController presentModalViewController:lvc animated:YES];
[self URL];
[lvc release];
return TRUE;
}
Now I do my parsing and when its done I call the following code in different view name XMLParsingView.m where the parsing got over.
- (void)handleLoadedApps
{
LoadingViewController *loading = [[[LoadingViewController alloc] init] autorelease];
//delegating to let the load view controller know to dimiss itself by defining disappear method in protocol
[loading.loadingDelegate disappear];
}
and in loading view controller I have method which calls dismissModalViewControlAnimated:
-(void)disappear{
[activity stopAnimating];
[activity removeFromSuperview];
[self removeFromSuperview];
[self dismissModalViewControllerAnimated:YES];
}
But for some reason it will never remove the view and not load it back to my tab bar controller.
Really need help here if any one have come across such issues.
Sagos
In your code you seem to create, without a nib, a new LoadingViewController and immediately go and dismiss it. In your app delegate you create your first loadingViewController with a nib, present it modally on rootController and then release it. Since you want to dismiss it outside your app delegate you have
3 choices, (hardest to fastest and most sane)
a) Key-Value-Observing on a property of XMLParsingView from LoadingViewController to remove itself when the task finishes.
b) Use delegation to inform the LoadingViewController when the task finishes to dismiss itself.
c) Fetch your rootController from your [[UIApplication sharedApplication] delegate], which means you must expose rootController as a property or through a method, and make rootController dismiss your modal.
You need to call dismissModalViewControllerAnimated on the rootViewController, not the loading view controller.

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];