【问题标题】:Creating a first launch viewcontroller创建第一个启动视图控制器
【发布时间】:2012-12-12 08:15:54
【问题描述】:

我正在尝试创建一个“初始设置页面”,如果应用程序首次在设备上启动,则会显示该页面。

我是这样设计的:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        NSLog(@"not first launch");
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;

    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];



        NSLog(@"first launch");

    }
}

现在我想创建一个视图控制器并在应用程序第一次启动时推送到该视图控制器。

我需要做什么?

【问题讨论】:

    标签: ios objective-c cocoa-touch uiviewcontroller


    【解决方案1】:

    创建一个新的 ViewController。在appDelegate.h 文件中导入头文件,同时创建一个名为initialViewController 的类的实例变量。

    改变你的 else 条件,如:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
       self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
       if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
       {
           NSLog(@"not first launch");
           self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
           self.window.rootViewController = self.viewController;
       }
       else
       {
           [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
           [[NSUserDefaults standardUserDefaults] synchronize];
    
           self.initialViewController = [[InitialViewController alloc] initWithNibName:@"InitialViewController" bundle:nil];
           self.window.rootViewController = self.InitialViewController;
           NSLog(@"first launch");
       }
       [self.window makeKeyAndVisible];
       return YES;
    }
    

    【讨论】:

    • 好吧,看起来不错,您建议创建视图控制器的最佳位置是什么?
    • @user1805901:我没听懂你!哪个视图控制器?
    • 我现在明白了;)非常感谢!
    • @user1805901:很高兴:)
    • @MidhunMP 嗨,这仅适用于情节提要吗?我无法让它适用于我不使用 Storyboards 的应用程序:S
    猜你喜欢
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多