Andrew
Andrew

Reputation: 552

How do I initiate a view control from AppDelegate?

Information

I am using Storyboard to add views. In my storyboard, I have 1 view controller and 1 Tab Bar Controller.

What I am trying to accomplish is a check to see whether or not the user is using my program for the first time, and if so, instead of showing the tab bar controller, it would show the initial view controller.

Here is my code that contains the error in AppDelegate.m at the [self presentViewController..].

The Storyboard ID for my Tab Bar Controller is 'TabBarController'.

Please note that ViewController.h & ViewController.m is UNTOUCHED.

Code

GlobalHeader.h

#ifndef dirt_GlobalHeader_h
#define dirt_GlobalHeader_h

BOOL RegisterCheck;

#endif

AppDelegate.h

#import <UIKit/UIKit.h>
#import "GlobalHeader.h"
#import "ViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    RegisterCheck = [[NSUserDefaults standardUserDefaults] boolForKey:@"RegisterCheck"];

    if (!RegisterCheck)
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"RegisterCheck"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else
    {
        UIStoryboard *MainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *TabBarController = [MainStoryBoard instantiateViewControllerWithIdentifier:@"TabBarController"];
        [self presentViewController:TabBarController animated:YES completion:nil];
    }
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

I thank anyone for any contributions towards my question.

I would also like to say that the Visual Studio IDE is much easier to use then this XCODE (that I am unfamiliar with).

Upvotes: 1

Views: 826

Answers (2)

suvish valsan
suvish valsan

Reputation: 869

the better way is to load the tabbarcontroller in both cases, check if the user runs app for first time in the first view-controller of tabbarcontroller ,then present the viewcontroller from inside

Upvotes: 1

Timothy Moose
Timothy Moose

Reputation: 9915

I think that instead of presenting the tab bar controller

[self presentViewController:TabBarController animated:YES completion:nil];

you should just be setting it as the root view controller (because you're not going to ever return to the initial view controller).

self.window.rootViewController = TabBarController;

Upvotes: 2

Related Questions