如何将bool值传递给下一个视图控制器

时间:2017-10-28 06:17:55

标签: ios swift xcode boolean

我有2 VC

FirstVc我有一个文字字段NamesTFsecondVC我需要检查FirstVc NamesTF中的值是否为bool。如果值可用,我的True值必须bool。如果值不可用,则False值必须FirstVc 我的代码:

let storyboard = UIStoryboard(name: "DashBoard", bundle: nil) let destinationVc = storyboard.instantiateViewController(withIdentifier: "SecondVC") if NamesTF.text == "" { SecondVC.NameAvailable = false self.navigationController?.pushViewController(destinationVc, animated: true) } else { SecondVC.NameAvailable = false self.navigationController?.pushViewController(destinationVc, animated: true) }

SecondVC

在我的 var NameAvailable = false override func viewWillAppear(_ animated: Bool) { if NameAvailable == true { // print the name } else { // no name availble //false } }

document.getElementById("copyButton1").addEventListener("click", function() {
  copyToClipboard(document.getElementById("copyTarget1"));
  $('.showclip1').addClass('showedclip');
});
document.getElementById("copyButton2").addEventListener("click", function() {
  copyToClipboard(document.getElementById("copyTarget2"));
  $('.showclip2').addClass('showedclip');
});
document.getElementById("copyButton3").addEventListener("click", function() {
  copyToClipboard(document.getElementById("copyTarget3"));
  $('.showclip3').addClass('showedclip');
});

我做错了什么。任何人都可以帮助我。

谢谢〜

3 个答案:

答案 0 :(得分:1)

你正在推动目的地'所以你的代码应该是:

    let storyboard = UIStoryboard(name: "DashBoard", bundle: nil)
    let destinationVc = storyboard.instantiateViewController(withIdentifier:"SecondVC")
    destinationVc.NameAvailable = !(NamesTF.text == "")
    self.navigationController?.pushViewController(destinationVc, animated: true)

答案 1 :(得分:1)

您的代码应该是这样的。 您在代码中所犯的错误是您将false值分配给 secondVC 中的Bool变量NameAvailable,因此无论天气如何{{1每次在 secondVC

中,您从 firstVc 传递的truefalse覆盖到false

第一次VC

let storyboard = UIStoryboard(name: "DashBoard", bundle: nil)
        let destinationVc = storyboard.instantiateViewController(withIdentifier: "SecondVC")

if NamesTF.text == "" {

 destinationVc.NameAvailable = true
 self.navigationController?.pushViewController(destinationVc, animated: true)
}

else 
{
 destinationVc.NameAvailable = false
 self.navigationController?.pushViewController(destinationVc, animated: true)
}

在你的第二个VC中

var NameAvailable = Bool()


override func viewWillAppear(_ animated: Bool) {

if NameAvailable == true {

// print the name

}
else {

// no name availble //false
}

尝试它肯定会起作用:)

答案 2 :(得分:1)

let storyboard = UIStoryboard(name: "DashBoard", bundle: nil)
let destinationVc = storyboard.instantiateViewController(withIdentifier:"SecondVC") as! SecondVC
destinationVc.NameAvailable = !(NamesTF.text == "")
self.navigationController?.pushViewController(destinationVc, animated: true)