Swift 3, Xcode 8 实例化视图控制器不工作
Swift 3, Xcode 8 Instantiate View Controller is not working
Xcode 8 编译时说用标识符更改实例化 viewcontroller
以简单地实例化视图控制器。我这样做了,为什么会报两个错误?
我正在使用 Swift 3. 我想更改页面 programmatically.I已经阅读了很多关于该主题的其他问题。它们都使用带有标识符的实例化视图控制器。他们还没有采用新语言。
@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)
}
谢谢。我按照建议更改了代码,只收到一个错误:可选类型 '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 vc = UIStoryboard(name: "ZWLStaticTabVc", bundle: nil).instantiateInitialViewController()
self.navigationController?.pushViewController(vc!, 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
在我的例子中,我忘记在情节提要中检查 isInitialViewController
我的 VC。因此,当我调用 instantiateInitialViewController 时它返回 nil。
在故事板中检查该选项后,问题解决了。
Xcode 8 编译时说用标识符更改实例化 viewcontroller
以简单地实例化视图控制器。我这样做了,为什么会报两个错误?
我正在使用 Swift 3. 我想更改页面 programmatically.I已经阅读了很多关于该主题的其他问题。它们都使用带有标识符的实例化视图控制器。他们还没有采用新语言。
@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)
}
谢谢。我按照建议更改了代码,只收到一个错误:可选类型 '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 vc = UIStoryboard(name: "ZWLStaticTabVc", bundle: nil).instantiateInitialViewController()
self.navigationController?.pushViewController(vc!, 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
在我的例子中,我忘记在情节提要中检查 isInitialViewController
我的 VC。因此,当我调用 instantiateInitialViewController 时它返回 nil。
在故事板中检查该选项后,问题解决了。