通过变量快速传递变量

问题描述 投票:0回答:1

我正在使用segue在xcode应用程序中打开新窗口。提示是正确的,当我点击按钮时,将显示新窗口。我想将一个字符串变量从FirstViewController传递给DetailViewController,但无法在DetailViewController中设置该变量。这是FirstViewController中的代码:

if control == view.rightCalloutAccessoryView {

            func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                        let controller = segue.destination as! DetailViewController
                        controller.Name = "test"
            }

            performSegue(withIdentifier: "showdetail", sender: self)
           }

这是DetailViewController代码

class DetailViewController: UIViewController, WKNavigationDelegate {
    var Name: String = ""
     override func viewDidLoad() {
      super.viewDidLoad()
      print(Name)
    }
}

我在哪里做错了?

swift segue
1个回答
2
投票

prepare(for方法必须在类的顶层。

并且请根据准则以小写字母开头来命名变量

func someMethod()
{
    if control == view.rightCalloutAccessoryView {
       performSegue(withIdentifier: "showdetail", sender: self)
    }
}

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   let controller = segue.destination as! DetailViewController
       controller.name = "test"
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.