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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24167073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:49:28  来源:igfitidea点击:

How to transition to a new view controller with code only using Swift

iosswift

提问by Stephen Fox

I want to transition from ViewController to secondViewController, when the user presses a UIButton, using code only in Swift.

我想从 ViewController 转换到 secondViewController,当用户按下 UIButton 时,仅使用 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)

 }

回答by rdelmar

The problem is that your code is creating a blank UIViewController, not a SecondViewController. You need to create an instance of your subclass, not a UIViewController,

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

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

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

 }

If you've overridden init(nibName nibName: String!,bundle nibBundle: NSBundle!) in your SecondViewController class, then you need to change the code to,

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

let sec: SecondViewController = SecondViewController(nibName: nil, bundle: nil)

回答by Stephen Fox

This worked perfectly for me:

这对我来说非常有效:

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

回答by Sazzad Hissain Khan

SWIFT

迅速

Usually for normal transition we use,

通常对于我们使用的正常过渡,

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

But sometimes when using navigation controller, you might face a black screen. In that case, you need to use like,

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

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

Moreover none of the above solution preserves navigationbar when you call from storyboard or single xib to another xib. If you use nav bar and want to preserve it just like normal push, you have to use,

此外,当您从故事板或单个 xib 调用另一个 xib 时,上述解决方案都不会保留导航栏。如果您使用导航栏并希望像普通推送一样保留它,则必须使用,

Let's say, "MyViewController" is identifier for MyViewController

假设“MyViewController”是 MyViewController 的标识符

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

回答by Lord Zsolt

Your code is just fine. The reason you're getting a black screen is because there's nothing on your second view controller.

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

Try something like:

尝试类似:

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

Now the view controller it shows should be red.

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

To actually do something with secondViewController, create a subclass of UIViewControllerand instead of

要实际执行某些操作secondViewController,请创建UIViewControllerand的子类而不是

let secondViewController:UIViewController = UIViewController()

create an instance of your second view controller:

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

//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

回答by DaveUT

For those using a second view controller with a storyboard in a .xib file, you will want to use the name of the .xib file in your constructor (without the.xib suffix).

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

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

回答by Garfonzo

For anyone doing this on iOS8, this is what I had to do:

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

I have a swift class file titled SettingsView.swiftand a .xib file named SettingsView.xib. I run this in MasterViewController.swift(or any view controller really to open a second view controller)

我有一个名为 swift 的类文件SettingsView.swift和一个名为.xib 的文件SettingsView.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)

    }

回答by Dmitriy Groschovskiy

In Swift 2.0 you can use this method:

在 Swift 2.0 中你可以使用这个方法:

    let registrationView = LMRegistration()
    self.presentViewController(registrationView, animated: true, completion: nil)

回答by Alok

Always use nibName file otherwise your preloaded content of Xib will not show .

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

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

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

回答by Matt Bart

Updated for Swift 3, some of these answers are a bit outdated.

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