我对Xcode有点新鲜。
我有一个关于在应用程序启动后加载其他内容的问题。我已经查看了过去的问题,例如我的问题,并在appdelegate.m
文件中看到了答案,特别是didFinishWithLaunchingOptions
方法。
但是,答案提供的代码对我来说没有任何补救措施,因为我使用的是storyboard
,而从Xcode 5开始,无法再使用initwithnib
方法。
如果我的问题不是很清楚,appdelegate.m
中的代码如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
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];
UIImage *navBackgroundImage = [UIImage imageNamed:@"nav_bg"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:10.0/255.0 green:10.0/255.0 blue:10.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Avenir Next" size:20.0], UITextAttributeFont, nil]];
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
return YES;
}
我在代码中遇到错误,如下所示:
所以,几乎我的问题是我在我的appdelegate.m
中尝试在我的方法中实现这个代码块,但是我遇到了一些错误,我不知道它们为什么会出现。< / p>
这也是我的两个视图控制器的图片,我希望它们是初始视图和应用程序加载一次后的视图:
如果它提供任何帮助:
第一个视图控制器是UIViewController
ViewController
(在身份检查器中,自定义类名为“Viewcontroller
”)ViewController.h
和.m文件。第二个视图控制器也是一个UIViewController
SWRevealViewController
(在身份检查器中,自定义类名为“SWRevealViewController
”)SWRevealViewController.h
和.m文件。答案 0 :(得分:2)
这个怎么样?
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
CustomVC *rootController = [storyboard instantiateViewControllerWithIdentifier:@"MyVC"];
self.window.rootViewController = rootController;
}
else{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
CustomVC *rootController = [storyboard instantiateViewControllerWithIdentifier:@"MyVC"];
self.window.rootViewController = rootController;
}
如果您需要更多帮助,请告诉我
答案 1 :(得分:2)
您可以使用restorationIdentifier,它位于Storyboard标识符的正上方,它是一个UIViewController属性。
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle: nil];
MyViewController *controller = (MyViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: @"<Controller ID>"];
self.window.rootViewController = controller;