2

I am working in iOS 5,and before loading my application,I want to open a another view controller,where the user should enter some data,for eg.password and when the password matches ,application will be opened,I am not getting how to do this..I tried some code ,which I have written below

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    if(somecondition)
    {       
        ViewController *View =[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; 
        [_window addSubview:View.view];
    }

    return YES;
}

But I dont know whether it is a right way,so friends,please help me out..

Regards Ranjit

Ranjit
  • 4,576
  • 11
  • 62
  • 121

2 Answers2

4

You should use

[self.window setRootViewController:yourViewController]

instead of addSubview to your window.
BTW, searching before asking is a good habit. ;)


If you want to show a view like the loginView or loadingView, you can set it as your rootViewController, when did loaded, you can reset your rootViewController.
Note, in your ProjectAppDelegate.m, you can get window by self.window, and in other child view controller's, you'll need

[[[UIApplication sharedApplication] delegate] window]

to get your main window.

Another simple way to meet your requirement is that you can just present a modalView before showing your app. Dismiss it after done and then start your app.

You can get more suggestion HERE.


BTW, I'm sorry I didn't get your comments' notification when you are write at other users comment area a few days ago. :( You should add @ before the user's name when you comment at somewhere else.

Community
  • 1
  • 1
Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • @Ranjit you also need add `[self.window makeKeyAndVisible];` before `return YES;`. You'll see the demo in the default `view based application` project of xCode. – Kjuly Nov 24 '11 at 13:40
  • @Ranjit `File` -> `New Project` -> `Application` -> `Single View Application`. – Kjuly Nov 24 '11 at 13:43
  • hey in iOS5 that all is not needed and by you one line code above I am able to open the view controller,before my app starts.now I want to know..after entering that view how can I star my app – Ranjit Nov 24 '11 at 13:48
  • @Ranjit Is not needed in iOS5.0? I'm sorry.:p But only for iOS5? Compatible for iOS4.0 is necessary I think. ;) – Kjuly Nov 24 '11 at 13:52
  • @Ranjit did you mean run the test app??? `Cmd + B` to build and `Cmd + R` to run. Maybe I misunderstood.. :P – Kjuly Nov 24 '11 at 13:54
  • no Kjuly..look,before starting my app I wanted to open a view controller,so as you said ,I wrote this line of code [self.window setRootViewController:yourViewController] in applicationlaunching and it works,now from the opened view controller,how shall I open my app – Ranjit Nov 24 '11 at 13:58
  • I'm sorry @Ranjit but I still not clear about "open my app". If you want to push new views to your app, you need set `UINavigationViewController` as your rootViewController instead of a common `UIViewController`, and then dispatch `pushViewController:animated:` whenever you want. You can layout some buttons, etc; But if you want to switch different view by some tabs, you need `UITabBarController`. – Kjuly Nov 24 '11 at 14:05
  • you are not getting it my friend – Ranjit Nov 24 '11 at 14:07
2

You can create some bool variable for checking is this a first start or another. The best place to store this bool is NSUserDefaults. Well, if this is a first start then show your LoginViewController, if not - execute regular code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    UIViewController *startVC = nil;

    if (isFirstLaunch){
        startVC = [[[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil] autorelease];
    }
    else{
        startVC = [[[WorkspaceViewController alloc] initWithNibName:@"WorkspaceView" bundle:nil] autorelease];
    }

    navController = [[UINavigationController alloc] initWithRootViewController:startVC];

    [self.window makeKeyAndVisible];
    [self.window addSubview:navController.view];

    return YES;
}
beryllium
  • 29,669
  • 15
  • 106
  • 125
  • hey beryllium,in my if condition,I wrote this line of code [self.window setRootViewController:yourViewController] and it opens a view controller,but now after entering this view controller,how shall I start my app ,any suggestions – Ranjit Nov 24 '11 at 13:52
  • [[(YourDelegate*)[[UIApplication sharedApplication] delegate] window] setRootViewController:yourAnotherViewController]; – beryllium Nov 24 '11 at 13:54
  • hey but i dont have any view controller,I created my application by selecting the option "Tab Bar application" in iOS 5 – Ranjit Nov 24 '11 at 13:57
  • tab bar is view controller as well. – beryllium Nov 24 '11 at 14:00
  • but I am not able to call that TabBar...?How to call it – Ranjit Nov 24 '11 at 14:03
  • in iOS 5 their is no declaration of TabBarController – Ranjit Nov 24 '11 at 14:09
  • @Ranjit if you set your TabBarController as the rootViewController, you can dispatch it by `YourCustomTabBarController * tabBarController = (YourCustomTabBarController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController];`(Just like @beryllium suggested), then use any method you want. – Kjuly Nov 24 '11 at 14:15
  • @Ranjit your UITabBarController is init with `xib` file if you "selecting the option 'Tab Bar application' in iOS 5 ". You can create a `IBOutlet UITabBarController * tabBarController`, then point it to your TabBarController in your Interface Builder. – Kjuly Nov 24 '11 at 14:20
  • Hey thanks Kjuly ,but I am confused,you tell me how can I call tabbarcontroller which will start my app – Ranjit Nov 24 '11 at 14:28
  • hey Kjuly..did you mean this way...... UITabBarController *tabBarcontroller = (UITabBarController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController]; [[(AppDelegate*)[[UIApplication sharedApplication] delegate] window] setRootViewController:tabBarcontroller]; – Ranjit Nov 24 '11 at 14:39