在 swift 中切换 ViewController,使用未解析的标识符
Switch ViewController in swift,use of unresolved identifier
XCode7.3Swift2.2
我想切换 3 ViewControllers:
我删除了默认的 ViewController.swift,并创建了一个 SwitchingViewController.swift(ViewController 的子类)。
这是我的代码:
blueViewController = stroryboard?.instantiateViewControllerWithIdentifier("Blue") as! BlueViewController
blueViewController.view.frame = view.frame
switchViewController(from: nil, to:blueViewController)
问题是“使用了未解析的标识符 'switchViewController'。
我尝试使用默认 ViewController.swift,但问题仍然存在。
我该怎么办?
谢谢。
您需要确保您要调用的所有内容都已声明。由于 Apple 未提供 switchViewController
,您需要自己创建。
如果您在 SwitchingViewController
class 中创建 func switchViewController(to:from:)
inside,您只需键入 switchViewController(to:from:)
即可调用它(使用值)。
如果你想从另一个 class 调用它,你需要在 在 中调用它 class ,就像这样: SwitchingViewController.switchViewController(to:from:)
(与值)。
希望对您有所帮助:)
P.S.: 如果你想做一个简单的过渡到ViewController,已经有苹果写的方法了。如果你使用故事板,你可以使用 performSegueWithIdentifier("")
.
所以,您正在使用 iPhone Development with Swift 这本书?他们在使用后定义了这个函数;他们的想法很愚蠢,因为它会引发错误,而且他们没有在文本中明确说明您稍后再写。
翻几页,你会看到他们定义了下面的函数。它可能会有所不同,具体取决于您的图书版本(我的是 swift 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)
}
}
XCode7.3Swift2.2
我想切换 3 ViewControllers:
我删除了默认的 ViewController.swift,并创建了一个 SwitchingViewController.swift(ViewController 的子类)。 这是我的代码:
blueViewController = stroryboard?.instantiateViewControllerWithIdentifier("Blue") as! BlueViewController
blueViewController.view.frame = view.frame
switchViewController(from: nil, to:blueViewController)
问题是“使用了未解析的标识符 'switchViewController'。
我尝试使用默认 ViewController.swift,但问题仍然存在。
我该怎么办?
谢谢。
您需要确保您要调用的所有内容都已声明。由于 Apple 未提供 switchViewController
,您需要自己创建。
如果您在 SwitchingViewController
class 中创建 func switchViewController(to:from:)
inside,您只需键入 switchViewController(to:from:)
即可调用它(使用值)。
如果你想从另一个 class 调用它,你需要在 在 中调用它 class ,就像这样: SwitchingViewController.switchViewController(to:from:)
(与值)。
希望对您有所帮助:)
P.S.: 如果你想做一个简单的过渡到ViewController,已经有苹果写的方法了。如果你使用故事板,你可以使用 performSegueWithIdentifier("")
.
所以,您正在使用 iPhone Development with Swift 这本书?他们在使用后定义了这个函数;他们的想法很愚蠢,因为它会引发错误,而且他们没有在文本中明确说明您稍后再写。
翻几页,你会看到他们定义了下面的函数。它可能会有所不同,具体取决于您的图书版本(我的是 swift 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)
}
}