0

我正在尝试制作一个我认为在使用前需要指南的应用程序,但是在重新启动应用程序时遇到问题,它会返回到入门视图而不是主视图(UIViewController)我有一个 plist 设置如果用户点击 Start App 它会保存一个布尔值,它会保存一个 YES 布尔值,但是当你第一次启动应用程序时,它默认设置为 A NO Bool。继承人来自 appDelegate 的代码 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"GetStartedCheck.plist"];

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
BOOL myBool = [[myDict objectForKey:@"alreadyUsedApp"] boolValue];
[myDict writeToFile:filePath atomically:YES];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
  if (myBool == NO)
   {
       self.startedViewController = [[GetStartedViewController alloc] initWithNibName:@"GetStartedViewController_iPhone" bundle:nil];
   }
   else if (myBool == YES)
   {
      self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
   }

} else {
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}

if (myBool == NO)
{
   self.window.rootViewController = self.startedViewController;
}
else if (myBool == YES)
{
self.window.rootViewController = self.viewController;
}
[self.window makeKeyAndVisible];
return YES;

有没有我可以阅读和遵循的教程或任何可以给我建议的人。

4

1 回答 1

0

您可以为此使用 NSUserDefaults,这很容易

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    BOOL myBool = YES;
    id exist = [[NSUserDefaults standardUserDefaults] objectForKey:@"FirstRun"];
    if(exist==nil){
        myBool = NO;
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"FirstRun"];
    }

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        if (myBool == NO)
        {
            self.startedViewController = [[GetStartedViewController alloc] initWithNibName:@"GetStartedViewController_iPhone" bundle:nil];
        }
        else if (myBool == YES)
        {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
        }

    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    }

    if (myBool == NO)
    {
        self.window.rootViewController = self.startedViewController;
    }
    else if (myBool == YES)
    {
        self.window.rootViewController = self.viewController;
    }
    [self.window makeKeyAndVisible];
    return YES;

}
于 2013-04-29T03:18:06.583 回答