Instantiate and Present a viewController in Swift

前端 未结 16 1552
难免孤独
难免孤独 2020-11-22 13:04

Issue

I started taking a look of the new Swift on Xcode 6, and I tried some demo projects and tutorials. Now I am stuck at:

16条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 13:17

    akashivskyy's answer works just fine! But, in case you have some trouble returning from the presented view controller, this alternative can be helpful. It worked for me!

    Swift:

    let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
    // Alternative way to present the new view controller
    self.navigationController?.showViewController(vc, sender: nil)
    

    Obj-C:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboardName" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"someViewController"];
    [self.navigationController showViewController:vc sender:nil];
    

提交回复
热议问题