我正在研究IOS开发,阅读一本名为IOS9 App Development Essentials的书。
当我尝试准备segue部分'有一个错误。我认为代码是从swift3改变的,我不知道怎么弄清楚。
代码正在关注
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destinationViewController as!
Scene2ViewControllerdestination.labelText = "arrived from scene1"
}
答案 0 :(得分:0)
您的代码不起作用,您应该使用Scene2ViewControllerdestination
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destinationViewController as! Scene2ViewControllerdestination
destination.labelText = "arrived from scene1"
}
答案 1 :(得分:0)
1)检查segue标识符(最佳实践)
2)创建目标视图控制器的对象
3)使用对象传递所需的数据
参考以下代码供您参考
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == YOUR_SEGUE_IDENTIFIER
{
let objSummery : SummeryVC = segue.destination as! SummeryVC
objSummery.strUserID = YOUR_VALUE
}
}
答案 2 :(得分:0)
Main.storyboard
文件中找到Segue标识符。
如果是,请使用类似这样的内容
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "your_identifier" {
let destinationVC = segue.destinationViewController as! UINavigationViewController
let sourceVC = destinationVC.viewControllers[0] as! Scene2ViewControllerdestination
sourceVC.labelText = "arrived from scene1"
}
}
如果没有,那么使用类似的东西
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "your_identifier" {
let destinationVC = segue.destinationViewController as! Scene2ViewControllerdestination
destinationVC.labelText = "arrived from scene1"
}
希望这会有所帮助。 :)
答案 3 :(得分:0)
您的代码段中有几个问题。
Scene2ViewController
和destination
之间缺少换行符,等等。)destinationViewController
已重命名为destination
。identifier
(当您有多个传出的段时,这将变得很重要)。请注意,您必须为您的segue设置标识符,如bhakti123所示。总而言之,这应该有效:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MeaningfulIdentifier",
let destination = segue.destination as? Scene2ViewController
{
destination.labelText = "arrived from scene1"
}
}