为了账号安全,请及时绑定邮箱和手机立即绑定

在appdelegate中设置初始viewcontroller-迅速

在appdelegate中设置初始viewcontroller-迅速

iOS
噜噜哒 2019-11-06 10:51:06
我想从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依赖于使用条件语句满足的某些条件。
查看完整描述

3 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

我使用此线程帮助我将目标C转换为swift,并且可以完美地工作。


在Swift中实例化并呈现一个viewController


Swift 2代码:


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)


    let storyboard = UIStoryboard(name: "Main", bundle: nil)


    let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginSignupVC")


    self.window?.rootViewController = initialViewController

    self.window?.makeKeyAndVisible()


    return true

}

Swift 3代码:


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    self.window = UIWindow(frame: UIScreen.main.bounds)


    let storyboard = UIStoryboard(name: "Main", bundle: nil)


    let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")


    self.window?.rootViewController = initialViewController

    self.window?.makeKeyAndVisible()


    return true

}


查看完整回答
反对 回复 2019-11-06
?
繁星淼淼

TA贡献1775条经验 获得超11个赞

尝试这个。例如:您应该UINavigationController用作初始视图控制器。然后,您可以将任何视图控制器设置为情节提要中的根。


 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Override point for customization after application launch.

    let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as UINavigationController

    let rootViewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("VC") as UIViewController

    navigationController.viewControllers = [rootViewController]

    self.window?.rootViewController = navigationController

    return true

}

看到我的故事板屏幕。


查看完整回答
反对 回复 2019-11-06
?
holdtom

TA贡献1805条经验 获得超10个赞

对于Swift 3,Swift 4:


从情节提要实例化根视图控制器:


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // this line is important

        self.window = UIWindow(frame: UIScreen.main.bounds)


        // In project directory storyboard looks like Main.storyboard,

        // you should use only part before ".storyboard" as it's name,

        // so in this example name is "Main".

        let storyboard = UIStoryboard.init(name: "Main", bundle: nil)


        // controller identifier sets up in storyboard utilities

        // panel (on the right), it called Storyboard ID

        let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController


        self.window?.rootViewController = viewController

        self.window?.makeKeyAndVisible()        

        return true

    }

如果要UINavigationController用作根用户:


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // this line is important

        self.window = UIWindow(frame: UIScreen.main.bounds)


        let storyboard = UIStoryboard.init(name: "Main", bundle: nil)

        let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController

        let navigationController = UINavigationController.init(rootViewController: viewController)

        self.window?.rootViewController = navigationController


        self.window?.makeKeyAndVisible()        

        return true

    }

从XIB实例化根视图控制器:


几乎一样,但是不是一行


let storyboard = UIStoryboard.init(name: "Main", bundle: nil)

let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as! YourViewController

你必须写


let viewController = YourViewController(nibName: "YourViewController", bundle: nil)


查看完整回答
反对 回复 2019-11-06
  • 3 回答
  • 0 关注
  • 1259 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信