在appdelegate中设置初始viewcontroller-迅速

我想从appdelegate设置初始viewcontroller。我找到了一个很好的答案,但是在目标C中,我无法迅速实现同一目标。


通过情节提要以编程方式设置初始视图控制器


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

{

    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];


    UIViewController *viewController = // determine the initial view controller here and instantiate   it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];


    self.window.rootViewController = viewController;

    [self.window makeKeyAndVisible];


    return YES;

}

有人能帮忙吗?


我希望初始Viewcontroller依赖于使用条件语句满足的某些条件。


噜噜哒
浏览 1260回答 3
3回答

三国纷争

我使用此线程帮助我将目标C转换为swift,并且可以完美地工作。在Swift中实例化并呈现一个viewControllerSwift 2代码:func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {&nbsp; &nbsp; self.window = UIWindow(frame: UIScreen.mainScreen().bounds)&nbsp; &nbsp; let storyboard = UIStoryboard(name: "Main", bundle: nil)&nbsp; &nbsp; let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginSignupVC")&nbsp; &nbsp; self.window?.rootViewController = initialViewController&nbsp; &nbsp; self.window?.makeKeyAndVisible()&nbsp; &nbsp; return true}Swift 3代码:func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {&nbsp; &nbsp; self.window = UIWindow(frame: UIScreen.main.bounds)&nbsp; &nbsp; let storyboard = UIStoryboard(name: "Main", bundle: nil)&nbsp; &nbsp; let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")&nbsp; &nbsp; self.window?.rootViewController = initialViewController&nbsp; &nbsp; self.window?.makeKeyAndVisible()&nbsp; &nbsp; return true}

繁星淼淼

尝试这个。例如:您应该UINavigationController用作初始视图控制器。然后,您可以将任何视图控制器设置为情节提要中的根。&nbsp;func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {&nbsp; &nbsp; // Override point for customization after application launch.&nbsp; &nbsp; let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)&nbsp; &nbsp; let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as UINavigationController&nbsp; &nbsp; let rootViewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("VC") as UIViewController&nbsp; &nbsp; navigationController.viewControllers = [rootViewController]&nbsp; &nbsp; self.window?.rootViewController = navigationController&nbsp; &nbsp; return true}看到我的故事板屏幕。

holdtom

对于Swift 3,Swift 4:从情节提要实例化根视图控制器:func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {&nbsp; &nbsp; &nbsp; &nbsp; // this line is important&nbsp; &nbsp; &nbsp; &nbsp; self.window = UIWindow(frame: UIScreen.main.bounds)&nbsp; &nbsp; &nbsp; &nbsp; // In project directory storyboard looks like Main.storyboard,&nbsp; &nbsp; &nbsp; &nbsp; // you should use only part before ".storyboard" as it's name,&nbsp; &nbsp; &nbsp; &nbsp; // so in this example name is "Main".&nbsp; &nbsp; &nbsp; &nbsp; let storyboard = UIStoryboard.init(name: "Main", bundle: nil)&nbsp; &nbsp; &nbsp; &nbsp; // controller identifier sets up in storyboard utilities&nbsp; &nbsp; &nbsp; &nbsp; // panel (on the right), it called Storyboard ID&nbsp; &nbsp; &nbsp; &nbsp; let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController&nbsp; &nbsp; &nbsp; &nbsp; self.window?.rootViewController = viewController&nbsp; &nbsp; &nbsp; &nbsp; self.window?.makeKeyAndVisible()&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; }如果要UINavigationController用作根用户:func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {&nbsp; &nbsp; &nbsp; &nbsp; // this line is important&nbsp; &nbsp; &nbsp; &nbsp; self.window = UIWindow(frame: UIScreen.main.bounds)&nbsp; &nbsp; &nbsp; &nbsp; let storyboard = UIStoryboard.init(name: "Main", bundle: nil)&nbsp; &nbsp; &nbsp; &nbsp; let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController&nbsp; &nbsp; &nbsp; &nbsp; let navigationController = UINavigationController.init(rootViewController: viewController)&nbsp; &nbsp; &nbsp; &nbsp; self.window?.rootViewController = navigationController&nbsp; &nbsp; &nbsp; &nbsp; self.window?.makeKeyAndVisible()&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; }从XIB实例化根视图控制器:几乎一样,但是不是一行let storyboard = UIStoryboard.init(name: "Main", bundle: nil)let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController你必须写let viewController = YourViewController(nibName: "YourViewController", bundle: nil)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS