我有2 VC
:
FirstVc
我有一个文字字段NamesTF
。
secondVC
我需要检查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');
});
我做错了什么。任何人都可以帮助我。
谢谢〜
答案 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
true
或false
覆盖到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)