首次运行时加载不同的视图

时间:2013-12-16 20:17:38

标签: ios objective-c view

我正在编写一个应用程序,该应用程序首次运行时需要一个不同的视图,用户可以在其中选择其首选设置。这是代码

实施 AppDelegate.m

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

// Determining Storyboard identifier for first view
// Determining Storyboard identifier for first view
NSString *storyboardID = [self hasEverBeenLaunched]? @"MainView" : @"LoginView";
// Setting proper view as a rootViewController
self.window.rootViewController = [self.window.rootViewController.storyboardinstantiateViewControllerWithIdentifier: @"view45"] ;

然后继续使用以下代码:

- (BOOL)hasEverBeenLaunched
{
// A boolean which determines if app has eer been launched
BOOL hasBeenLaunched;

// Testig if application has launched before and if it has to show the home-login screen        to login
// to social networks (facebook, Twitter)
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasAlreadyLaunched"]) {
// Setting variable to YES because app has been launched before
hasBeenLaunched = YES;
 NSLog(@"App has been already launched");
} else {
// Setting variable to NO because app hasn't been launched before
hasBeenLaunched = NO;

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasAlreadyLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
 NSLog(@"This is the first run ever...");
}

return hasBeenLaunched;
}

只需几个快速注释:View 45是初始启动视图,只能显示一次,否则主视图控制器会在atrributes下被勾选为初始视图控制器(始终在启动后加载的视图控制器)第一次运行)

所以问题是它只会加载视图45,第一次运行视图,但是什么导致了这一点?

1 个答案:

答案 0 :(得分:2)

问题出在以下几行:

self.window.rootViewController = [self.window.rootViewController.storyboardinstantiateViewControllerWithIdentifier: @"view45"] ;

它忽略了storyboardID的值,只是使用了字符串@"view45",这就是为什么你每次都会得到同一个。

如果要修复它,请将其更改为以下行:

self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardID];

您现在可以看到它正在使用storyboardID中的值。