在swift中切换ViewController,使用未解析的标识符

时间:2022-12-01 06:30:53

XCode 7.3 Swift 2.2

XCode 7.3 Swift 2.2

I wanted to switch 3 ViewControllers:

我想切换3个ViewControllers:

I deleted the default ViewController.swift,and created a SwitchingViewController.swift(subclass of ViewController). This is my code:

我删除了默认的ViewController.swift,并创建了一个SwitchingViewController.swift(ViewController的子类)。这是我的代码:

blueViewController = stroryboard?.instantiateViewControllerWithIdentifier("Blue") as! BlueViewController
blueViewController.view.frame = view.frame
switchViewController(from: nil, to:blueViewController)

The problem is "Use of unresolved identifier 'switchViewController'.

问题是“使用未解析的标识符'switchViewController'。

I tried to use the default ViewController.swift,and I still had the problem.

我试图使用默认的ViewController.swift,但我仍然遇到了问题。

How can I do?

我能怎么做?

Thanks.

2 个解决方案

#1


0  

You need to make sure everything you want to call has been declared yet. Since switchViewController is not provided by Apple you will need to create it yourself.

您需要确保已声明要调用的所有内容。由于Apple不提供switchViewController,您需要自己创建它。

If you create the func switchViewController(to:from:) inside the SwitchingViewController class you can simply call it by typing switchViewController(to:from:) (with the values).
If you want to call it from another class, you need to call it on the class it's declared in, like this: SwitchingViewController.switchViewController(to:from:) (with the values).

如果在SwitchingViewController类中创建func switchViewController(to:from :),只需键入switchViewController(to:from :)(带有值)即可调用它。如果你想从另一个类调用它,你需要在它声明的类中调用它,如下所示:SwitchingViewController.switchViewController(to:from :)(带有值)。

I hope that helps :)

我希望有帮助:)

P.S.: If you want to do a simple transition to a ViewController, there are already methods written by Apple. If you use storyboards, you can for instance use performSegueWithIdentifier("").

P.S。:如果你想简单地过渡到ViewController,已经有Apple编写的方法。如果您使用故事板,则可以使用performSegueWithIdentifier(“”)。

#2


0  

So, you're using the book iPhone Development with Swift? They define this function after using it; pretty silly idea on their part since it's going to throw an error and they didn't make it clear in the text that you write it later.

那么,你正在使用iPhone开发和Swift这本书吗?它们在使用后定义了这个功能;这是非常愚蠢的想法,因为它会抛出一个错误,他们没有在文本中明确表示你以后写的。

Flip a few pages and you will see that they define the below function. It might be different depending on your version of the book (mine is swift 2)

翻转几页,您将看到他们定义了以下功能。它可能会有所不同,具体取决于您的书籍版本(我的版本很快2)

    private func switchViewController(from fromVC:UIViewController?, to toVC:UIViewController?){
    if fromVC != nil {
        fromVC!.willMoveToParentViewController(nil)
        fromVC!.view.removeFromSuperview()
        fromVC!.removeFromParentViewController()

    }

    if toVC != nil{
        self.addChildViewController(toVC!)
        self.view.insertSubview(toVC!.view, atIndex: 0)
        toVC!.didMoveToParentViewController(self)
    }

}

#1


0  

You need to make sure everything you want to call has been declared yet. Since switchViewController is not provided by Apple you will need to create it yourself.

您需要确保已声明要调用的所有内容。由于Apple不提供switchViewController,您需要自己创建它。

If you create the func switchViewController(to:from:) inside the SwitchingViewController class you can simply call it by typing switchViewController(to:from:) (with the values).
If you want to call it from another class, you need to call it on the class it's declared in, like this: SwitchingViewController.switchViewController(to:from:) (with the values).

如果在SwitchingViewController类中创建func switchViewController(to:from :),只需键入switchViewController(to:from :)(带有值)即可调用它。如果你想从另一个类调用它,你需要在它声明的类中调用它,如下所示:SwitchingViewController.switchViewController(to:from :)(带有值)。

I hope that helps :)

我希望有帮助:)

P.S.: If you want to do a simple transition to a ViewController, there are already methods written by Apple. If you use storyboards, you can for instance use performSegueWithIdentifier("").

P.S。:如果你想简单地过渡到ViewController,已经有Apple编写的方法。如果您使用故事板,则可以使用performSegueWithIdentifier(“”)。

#2


0  

So, you're using the book iPhone Development with Swift? They define this function after using it; pretty silly idea on their part since it's going to throw an error and they didn't make it clear in the text that you write it later.

那么,你正在使用iPhone开发和Swift这本书吗?它们在使用后定义了这个功能;这是非常愚蠢的想法,因为它会抛出一个错误,他们没有在文本中明确表示你以后写的。

Flip a few pages and you will see that they define the below function. It might be different depending on your version of the book (mine is swift 2)

翻转几页,您将看到他们定义了以下功能。它可能会有所不同,具体取决于您的书籍版本(我的版本很快2)

    private func switchViewController(from fromVC:UIViewController?, to toVC:UIViewController?){
    if fromVC != nil {
        fromVC!.willMoveToParentViewController(nil)
        fromVC!.view.removeFromSuperview()
        fromVC!.removeFromParentViewController()

    }

    if toVC != nil{
        self.addChildViewController(toVC!)
        self.view.insertSubview(toVC!.view, atIndex: 0)
        toVC!.didMoveToParentViewController(self)
    }

}