如何从AppDelegate启动视图控件?

时间:2013-11-11 00:39:17

标签: ios cocoa-touch uiviewcontroller storyboard

信息

我正在使用Storyboard添加视图。在我的故事板中,我有一个视图控制器和一个标签栏控制器。

我想要完成的是检查用户是否第一次使用我的程序,如果是,则不显示标签栏控制器,而是显示初始视图控制器。

这是我的代码,其中包含[self presentViewController ..]中AppDelegate.m中的错误。

我的标签栏控制器的故事板ID是'TabBarController'。

请注意ViewController.h& ViewController.m是UNTOUCHED。

代码

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

我感谢任何人对我的问题的任何贡献。

我还想说Visual Studio IDE比这个XCODE(我不熟悉)更容易使用。

2 个答案:

答案 0 :(得分:2)

我认为不是呈现标签栏控制器

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

你应该将它设置为根视图控制器(因为你不会再回到初始视图控制器)。

self.window.rootViewController = TabBarController;

答案 1 :(得分:1)

更好的方法是在两种情况下加载tabbarcontroller,检查用户是否第一次在tabbarcontroller的第一个视图控制器中运行app,然后从内部呈现viewcontroller