如何使用Swift仅使用代码转换到新的视图控制器

时间:2014-06-11 15:37:22

标签: ios swift

当用户按下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)

 }

9 个答案:

答案 0 :(得分:57)

问题是您的代码正在创建一个空白的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)

答案 1 :(得分:35)

这对我很有用:

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)
    }
}

答案 2 :(得分:14)

<强> SWIFT

通常我们使用正常过渡,

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时,上述解决方案都不会保留导航栏。如果您使用导航栏并希望像普通推送一样保留它,则必须使用

让我们说,&#34; MyViewController&#34;是MyViewController的标识符

let viewController = MyViewController(nibName: "MyViewController", bundle: nil)
self.navigationController?.pushViewController(viewController, animated: true)

答案 3 :(得分:9)

你的代码很好。你得到黑屏的原因是你的第二个视图控制器没有任何东西。

尝试类似:

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

答案 4 :(得分:3)

对于那些在.xib文件中使用带有故事板的第二个视图控制器的人,您将需要在构造函数中使用.xib文件的名称(不带.xib后缀)。

let settings_dialog:SettingsViewController = SettingsViewController(nibName: "SettingsViewController", bundle: nil)
self.presentViewController(settings_dialog, animated: true, completion: nil)

答案 5 :(得分:2)

对于在iOS8上这样做的人来说,这就是我必须要做的事情:

我有一个标题为SettingsView.swift的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)

    }

答案 6 :(得分:2)

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

rend()

答案 7 :(得分:1)

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

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

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

答案 8 :(得分:1)

针对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)    }