如何在首次启动应用程序时显示UIViewController?

时间:2012-01-11 04:41:06

标签: iphone

我想知道如何在第一次启动应用程序时显示一个页面,我有这些并获得许多代码,但在我的情况下它不适用于我,我在DidFinishLaunching方法中有这个代码,< / p>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]];

    //If First Launch
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        //Show help view
        UIScrollView_PagingViewController *detailViewController = [[UIScrollView_PagingViewController alloc] initWithNibName:@"UIScrollView_PagingViewController" bundle:nil];

        //detailViewController.firstString = firstString;
        // ...
        // Pass the selected object to the new view controller.

        [self.navigationController pushViewController:detailViewController animated:YES];

        [detailViewController release];

    }
    else {
    NSError *error = nil;
    NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
    NSString *str =  [SFHFKeychainUtils getPasswordForUsername:username andServiceName:@"mybibleapp" error:&error];
    NSLog(@"previous user");

    NSLog(@"%@", str);

    if (!error && nil != str)
    {
        ParallelReadViewController *detailViewController = [[ParallelReadViewController alloc] initWithNibName:@"ParallelReadViewController" bundle:nil];

        //detailViewController.firstString = firstString;
        // ...
        // Pass the selected object to the new view controller.

        [self.navigationController pushViewController:detailViewController animated:YES];

        [detailViewController release];
    }
    else
    {
        RootViewController *detailViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

        //detailViewController.firstString = firstString;
        // ...
        // Pass the selected object to the new view controller.

        [self.navigationController pushViewController:detailViewController animated:YES];

        [detailViewController release]; 
    }
}

但是当我运行此代码时,它显示一个空白的白色屏幕,没有任何显示。

[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]];

        //If First Launch
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
            //Show help view
            UIScrollView_PagingViewController *detailViewController = [[UIScrollView_PagingViewController alloc] initWithNibName:@"UIScrollView_PagingViewController" bundle:nil];

            //detailViewController.firstString = firstString;
            // ...
            // Pass the selected object to the new view controller.

            [self.navigationController pushViewController:detailViewController animated:YES];

            [detailViewController release];

        }
        else {

我把上面的代码用于启动UIScrollView_PagingViewController来启动。但没有运气。我在上面的代码中犯了什么错误。请帮助我。 提前谢谢。

4 个答案:

答案 0 :(得分:0)

您没有将代码放在正确的位置。不要将此代码放在AppDelegate中,尽量保持appdelegate尽可能干净,以获得更好的@performance。使用一些ViewController,它将在启动画面后调用,并检查您的代码是否在那里工作。

答案 1 :(得分:0)

你正试图在第一时间推动该控制器,但在roo没有任何控制器。所以你必须先在窗口中添加。然后你可以推UIViewController

self.window.rootViewController = detailViewController.view;

在使用之前不要发布viewController。

答案 2 :(得分:0)

试试这个

        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) 
    {

     UIScrollView_PagingViewController *detailViewController =[[UIScrollView_PagingViewController alloc] initWithNibName:@"UIScrollView_PagingViewController" bundle:nil];

                    //detailViewController.firstString = firstString;
                    // ...
                    // Pass the selected object to the new view controller.

   self.navController=[[UINavigationController alloc] initWithRootViewController:detailViewController];
    self.window.rootViewController=self.navController;

                }

答案 3 :(得分:0)

做一个trcik, 不要将此代码放在AppDelegate中,尽量保持appdelegate尽可能干净,以获得更好的性能。

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

    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

并将此代码放入RootViewController 每次应用程序启动时,都会检查委托方法中的条件,而不是检查RootViewController。

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:YES];
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]];

    //If First Launch
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        //Show help view
        UIScrollView_PagingViewController *detailViewController = [[UIScrollView_PagingViewController alloc] initWithNibName:@"UIScrollView_PagingViewController" bundle:nil];

        //detailViewController.firstString = firstString;
        // ...
        // Pass the selected object to the new view controller.

        [self.navigationController pushViewController:detailViewController animated:NO];

        [detailViewController release];

    }
    else {
    NSError *error = nil;
    NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
    NSString *str =  [SFHFKeychainUtils getPasswordForUsername:username andServiceName:@"mybibleapp" error:&error];
    NSLog(@"previous user");

    NSLog(@"%@", str);

    if (!error && nil != str)
    {
        ParallelReadViewController *detailViewController = [[ParallelReadViewController alloc] initWithNibName:@"ParallelReadViewController" bundle:nil];

        //detailViewController.firstString = firstString;
        // ...
        // Pass the selected object to the new view controller.

        [self.navigationController pushViewController:detailViewController animated:NO];

        [detailViewController release];
    }
    else
    {
       // RootViewController *detailViewController = [[RootViewController alloc] //initWithNibName:@"RootViewController" bundle:nil];

        //detailViewController.firstString = firstString;
        // ...
        // Pass the selected object to the new view controller.

       // [self.navigationController pushViewController:detailViewController //animated:NO];

        //[detailViewController release]; 
    }
}