我正在使用swift开发一个简单的IOS应用程序。在我的应用程序中,我需要从另一个控制器以编程方式打开新控制器。所以我在故事板上添加了另一个场景。
然后我为新控制器添加了一个新类,它继承自UIViewController。这是新控制器的代码
import UIKit
class ReplayController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
然后,我尝试在代码中从主控制器打开新的控制器视图(ReplayController)。
func gameOver()
{
let replayController = ReplayController()
present(replayController, animated: true, completion: nil)
}
当我调用该功能时,它只会弹出空白屏幕。屏幕上没有任何内容。有什么问题,我该如何解决?
答案 0 :(得分:2)
您必须在故事板中使用id引用它
let replayController = self.storyboard?.instantiateViewController(withIdentifier: "replayControllerID") as! ReplayController
present(replayController, animated: true, completion: nil)
仅作为此行
let replayController = ReplayController()
不加载与VC关联的xib或storyboard对象
答案 1 :(得分:2)
在您的情节提要文件中,确保ReplayController
拥有其文件所有者'设置为ReplayController
课程。然后设置如下所示的故事板ID:
然后你可以这样加载它:
let replay = storyboard?.instantiateViewController(withIdentifier: "ReplayController") as! ReplayController
self.present(replay, animated: true, completion: nil)
答案 2 :(得分:1)