![](/img/trans.png)
[英]Xcode & Swift - unable to instantiate another view controller from AppDelegate
[英]Swift 3, Xcode 8 Instantiate View Controller is not working
的xcode它編譯時8表示改變實例化viewcontroller
具有標識符簡單地,實例視圖控制器。 我這樣做了,為什么會出現兩個錯誤?
我正在使用 Swift 3。我想以編程方式更改頁面。我已經閱讀了很多關於該主題的其他問題。 他們都使用帶有標識符的實例化視圖控制器。 他們還沒有采用新的語言。
@IBAction func switchPage(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController =
storyboard.instantiateViewController("secondViewController") as!
UIViewController
self.presentViewController(secondViewController, animated: true,
completion: nil)
}
謝謝。 我按照建議更改了代碼,但只收到一個錯誤:Value of optional type 'UIStoryboard?' 未解開; 你的意思是使用“!” 或'?'? 我應該在某處添加感嘆號嗎?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a
nib.
}
@IBAction func ch(_ sender: UIButton) {
let viewController =
storyboard.instantiateViewController(withIdentifier:
"secondViewController") as! UIViewController
self.present(viewController, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
像這樣嘗試。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"secondViewController") as! UIViewController
self.present(viewController, animated: true)
它對我有用:
let gameScene = UIStoryboard(name: "Main", bundle:nil).instantiateViewController(withIdentifier: "ViewController") as UIViewController
let appDelegate = (UIApplication.shared.delegate as! AppDelegate)
appDelegate.window?.rootViewController = gameScene
它對我有用:
let vc = UIStoryboard(name: "ZWLStaticTabVc", bundle: nil).instantiateInitialViewController()
self.navigationController?.pushViewController(vc!, animated: true)
就我而言,我忘記在情節isInitialViewController
中檢查我的 VC 的isInitialViewController
。 因此,當我調用instantiateInitialViewController 時它返回nil。 在故事板中檢查該選項后,問題解決了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.