0

(Xcode 6.4) How can i set initial view controller by code? (I want to set initial view controller for a first launch using this code:

if (![[NSUserDefaults standardUserDefaults] valueForKey:@"firstRunCompleted"])
{
     //Here i want to set the initial view

}

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstRunCompleted"];
[[NSUserDefaults standardUserDefaults] synchronize];
  • http://stackoverflow.com/questions/10428629/programatically-set-the-initial-view-controller-using-storyboards – 72A12F4E Aug 11 '15 at 19:39

2 Answers2

1

Assuming you are in appDelegate.m in didFinishLaunchingWithOptions method

self.window.rootViewController = [ViewController new];

Replace ViewController with whatever custom View Controller you want to set as your initial View. If you want to imbed in a navigation controller, which is common,

self.window.rootViewController = [UINavigationController alloc] initWithRootViewController:[ViewController new]];

NSGangster
  • 2,307
  • 8
  • 22
0

In AppDelegate.m Try this :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (![[NSUserDefaults standardUserDefaults] valueForKey:@"firstRunCompleted"])
    {
      [self setUpRootViewController];
    }

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstRunCompleted"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return YES;
}

-(void)setUpRootViewController{
    //Check if window is nil, if YES initialize the window as below
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];

    ViewController* viewControllerObj = [[ViewController alloc] init];
    viewControllerObj.view = [[UIView alloc] initWithFrame:self.window.frame];
    viewControllerObj.view.backgroundColor = [UIColor whiteColor];
    [viewControllerObj setUpViewController];
    self.window.rootViewController = viewControllerObj;

}
Chengappa C D
  • 1,653
  • 1
  • 9
  • 18