-1

I have 2 VC :

FirstVc in that i have one textfield NamesTF . secondVC i need to check whether in FirstVc the NamesTF have values are not. If values is available my bool value have to come True .If values is not available bool value have to come False My code :

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)
}

In my SecondVC:

 var NameAvailable = false

override func viewWillAppear(_ animated: Bool) {

if NameAvailable == true {

// print the name

}
else {

// no name availble //false
}
}

What i am doing wrong. Can any one help me out.

Thanks ~

david
  • 636
  • 2
  • 12
  • 29

3 Answers3

1

You are pushing the 'destinationVc' so your code should be like:

    let storyboard = UIStoryboard(name: "DashBoard", bundle: nil)
    let destinationVc = storyboard.instantiateViewController(withIdentifier:"SecondVC")
    destinationVc.NameAvailable = !(NamesTF.text == "")
    self.navigationController?.pushViewController(destinationVc, animated: true)
Abhishek Thapliyal
  • 3,497
  • 6
  • 30
  • 69
SandeepM
  • 2,601
  • 1
  • 22
  • 32
1

You code should be something like this. The mistake you are doing in your code is that you are assigning false value to the Bool variable NameAvailable in your secondVC, so whatever calue weather true or false you are passing from firstVc is overriding to false every time in the secondVC

First 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)
}

In Your Second VC

var NameAvailable = Bool()


override func viewWillAppear(_ animated: Bool) {

if NameAvailable == true {

// print the name

}
else {

// no name availble //false
}

Try it it will definitely work :)

Deepraj Chowrasia
  • 1,349
  • 10
  • 20
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)
Sharad Paghadal
  • 2,089
  • 15
  • 29