【问题标题】:how to pass the bool values to next view controller如何将布尔值传递给下一个视图控制器
【发布时间】:2018-04-09 18:08:32
【问题描述】:

我有 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
}
}

我做错了什么。谁能帮帮我。

谢谢~

【问题讨论】:

  • 这些SecondVCAllOperatorNewVC 来自哪里?
  • @Adeel 抱歉,这是我的代码错误。现在我有一个正确的。请查看我的帖子。
  • 这仍然没有意义。您在SecondVC 中设置Bool 并推送destinationVc。在destinationVc 中设置布尔值,像这样destinationVc.NameAvailable = true
  • @david 传递值的概念永远不会改变 bool、string、array 等。

标签: ios swift xcode boolean


【解决方案1】:

您正在推送“destinationVc”,因此您的代码应如下所示:

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

【讨论】:

  • 我收到类似instance member 'NameAvailable' cannot be used on type 'secondVC的错误
  • 您在哪里定义了 'CircleNameAvailable' ?在 SecondVC 中,您定义了“NameAvailable”。
  • 对不起var NameAvailable : Bool?就像我在我的secondVC中声明的那样
  • @Abhishek Thapliyal NameAvailable 在我的seconsVC 中应该是bool 值或string
  • 它应该是 Bool @david
【解决方案2】:

你的代码应该是这样的。 您在代码中犯的错误是您将 false 值分配给 secondVC 中的 Bool 变量 NameAvailable,所以无论您是 truefalse每次在 secondVC

中,从 firstVc 传递都会覆盖到 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
}

试试吧,肯定会成功的:)

【讨论】:

    【解决方案3】:
    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      相关资源
      最近更新 更多