ios-如何仅使用Swi转换为带有代码的新视图控制器

我想在用户按下UIButton时从ViewController过渡到secondViewController,仅在Swift中使用代码。

//Function to transition
func transition(Sender: UIButton!)
{   
    //Current Code, default colour is black
    let secondViewController:UIViewController = UIViewController()
    self.presentViewController(secondViewController, animated: true, completion: nil)

 }
Stephen Fox asked 2020-02-18T06:29:28Z
9个解决方案
61 votes

问题在于您的代码创建的是空白的UIViewController,而不是SecondViewController。 您需要创建子类的实例,而不是UIViewController,

func transition(Sender: UIButton!) {   
    let secondViewController:SecondViewController = SecondViewController()

    self.presentViewController(secondViewController, animated: true, completion: nil)

 }

如果您在SecondViewController类中重写了init(nibName nibName:String!,bundle nibBundle:NSBundle!),则需要将代码更改为

let sec: SecondViewController = SecondViewController(nibName: nil, bundle: nil)
rdelmar answered 2020-02-18T06:29:47Z
36 votes

这对我来说非常合适:

func switchScreen() {
    let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "yourVcName") as? UIViewController {
        self.present(viewController, animated: true, completion: nil)
    }
}
OkiRules answered 2020-02-18T06:30:07Z
15 votes

迅速

通常为了正常过渡,我们使用

let next:SecondViewController = SecondViewController()
self.presentViewController(next, animated: true, completion: nil)

但是有时使用导航控制器时,您可能会遇到黑屏。 在这种情况下,您需要使用like,

let next:ThirdViewController = storyboard?.instantiateViewControllerWithIdentifier("ThirdViewController") as! ThirdViewController
self.navigationController?.pushViewController(next, animated: true)

此外,当您从情节提要或单个xib调用到另一个xib时,以上解决方案都无法保留导航栏。 如果您使用导航栏,并且希望像普通推送一样保留它,则必须使用,

假设“ MyViewController”是MyViewController的标识符

let viewController = MyViewController(nibName: "MyViewController", bundle: nil)
self.navigationController?.pushViewController(viewController, animated: true)
Sazzad Hissain Khan answered 2020-02-18T06:30:46Z
9 votes

您的代码很好。 黑屏的原因是第二个视图控制器上没有任何内容。

尝试类似的东西:

secondViewController.view.backgroundColor = UIColor.redColor();

现在它显示的视图控制器应该是红色的。

要对secondViewController进行实际操作,请创建UIViewController的子类,而不是

let secondViewController:UIViewController = UIViewController()

创建第二个视图控制器的实例:

//If using code
let secondViewController = MyCustomViewController.alloc()

//If using storyboard, assuming you have a view controller with storyboard ID "MyCustomViewController"
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("MyCustomViewController") as UIViewController
Lord Zsolt answered 2020-02-18T06:31:23Z
3 votes

对于使用第二个视图控制器和.xib文件中的情节提要的用户,您将需要在构造函数中使用.xib文件的名称(不带.xib后缀)。

let settings_dialog:SettingsViewController = SettingsViewController(nibName: "SettingsViewController", bundle: nil)
self.presentViewController(settings_dialog, animated: true, completion: nil)
DaveUT answered 2020-02-18T06:31:43Z
2 votes

对于在iOS8上执行此操作的任何人,这就是我要做的:

我有一个名为SettingsView.swift的快速类文件,一个名为SettingsView.xib的.xib文件。我在MasterViewController.swift中运行了该文件(或任何打开了第二个视图控制器的视图控制器)

@IBAction func openSettings(sender: AnyObject) {
        var mySettings: SettingsView = SettingsView(nibName: "SettingsView", bundle: nil) /<--- Notice this "nibName" 
        var modalStyle: UIModalTransitionStyle = UIModalTransitionStyle.CoverVertical
        mySettings.modalTransitionStyle = modalStyle
        self.presentViewController(mySettings, animated: true, completion: nil)

    }
Garfonzo answered 2020-02-18T06:32:08Z
2 votes

在Swift 2.0中,您可以使用以下方法:

    let registrationView = LMRegistration()
    self.presentViewController(registrationView, animated: true, completion: nil)
Dmitriy Groschovskiy answered 2020-02-18T06:32:28Z
1 votes

请始终使用nibName文件,否则Xib的预加载内容将不会显示。

vc : ViewController =  ViewController(nibName: "ViewController", bundle: nil) //change this to your class name

 self.presentViewController(vc, animated: true, completion: nil)
Alok answered 2020-02-18T06:32:49Z
1 votes

已针对Swift 3更新,其中一些答案有些过时了。

let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let vc : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "myStoryboardID") as UIViewController
        self.present(vc, animated: true, completion: nil)    }
Matt Bart answered 2020-02-18T06:33:09Z
translate from https://stackoverflow.com:/questions/24167073/how-to-transition-to-a-new-view-controller-with-code-only-using-swift