0

I need to send bool value to Second viewController based on some condition. but in Second ViewController the value of bool variable is always false. This is First ViewController code:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
@IBAction func moveTo(_ sender: Any) {
    self.performSegue(withIdentifier: "Move2", sender: self)

}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.identifier == "Move2"
    {
        let secondVC = segue.destination as? SecondViewController
        secondVC?.second = true

    }
}
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
}

The SecondViewController code is

import UIKit

class SecondViewController: UIViewController {
var second = Bool()

override func viewDidLoad() {
    super.viewDidLoad()
    print(second)
    // Do any additional setup after loading the view.
}

But this Value of second variable is false.

Whats wrong in my code. need help. TIA..

EDIT : In both ViewController i have navigation controller. That's problem. SecondVC is SecondViewController's navigation controller, so that i can't pass data to that secondVC.

Vimalkumar N.M.
  • 493
  • 2
  • 4
  • 16

2 Answers2

1

Go to your Storyboard and select SecondViewController (the yellow circle on the top left. Go to identity inspector and select SecondViewController from the Class drop down.

See image below:

enter image description here

Martin Muldoon
  • 3,388
  • 4
  • 24
  • 55
  • in both viewcontroller it contains Navigation Controller. I removed navigation Controller in second VC. now its working fine.. is that problem? – Vimalkumar N.M. Apr 11 '18 at 13:57
  • If the SecondViewController was embedded in a Navigation Controller it would cause an issue. Typically your First VC is embedded and that's it. – Martin Muldoon Apr 11 '18 at 14:02
0

Keep things simple and clean :

FirstVC :

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.identifier == "Move2"
        {
            let secondVC = segue.destination as? SecondViewController
            secondVC.second = true

        }
    } 

SecondVC :

class SecondViewController: UIViewController {
    var second: Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
        print(second)
    }  
}  

Just tested this myself. true is printed.

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • `secondVC?.second = true` - Is optional – Scriptable Apr 11 '18 at 13:44
  • How about `if let secondVC = ...`, `secondVC` is an optional. – bauerMusic Apr 11 '18 at 13:44
  • @Scriptable : I don't see any need for optional. – Nitish Apr 11 '18 at 13:45
  • 2
    `let secondVC = segue.destination as? SecondViewController` as? creates optional – Scriptable Apr 11 '18 at 13:46
  • You can change `as?` to `as!` which is a perfectly valid coding decision. You'd want it to crash if you didn't have the segue wired to the correct VC type. – vacawama Apr 11 '18 at 13:47
  • @Scriptable : Have you tried that ? There is **no** need of taking that optional in secondVC?.second = true – Nitish Apr 11 '18 at 13:47
  • @vacawama I have to disagree, allowing a crash is not a valid coding decision – Scriptable Apr 11 '18 at 13:49
  • @Scriptable, not having your segue wired to the correct type is a programming error not a runtime error. It's the same as not wiring up an outlet. You want that to crash so that you find the problem. – vacawama Apr 11 '18 at 13:51
  • @Nitish its not a big deal, just trying to help improve your answer. `let vc = SecondViewController() let secondVC = vc as? SecondViewController print(secondVC)` prints an optional. xcode autocorrects `secondVC.second = true` to `secondVC?.second = true` try it – Scriptable Apr 11 '18 at 13:52
  • You cannot have `as?` casting returning a non optional. – bauerMusic Apr 11 '18 at 13:57
  • in both viewcontroller it contains Navigation Controller. I removed navigation Controller in second VC. now its working fine.. is that problem? – Vimalkumar N.M. Apr 11 '18 at 13:58
  • @VimalkumarN.M. No it shouldn't cause any issues unless you need a nav controller there – Scriptable Apr 11 '18 at 14:00
  • @Nitish @Scriptable what is the differences between `var myBool = Bool()` and `var myBool = false` both are initialized with false boolean value. – Kazi Abdullah Al Mamun Apr 11 '18 at 14:04
  • @Scriptable, if he has used `as!`, it would have crashed saying `UINavigationController` couldn't be cast as `SecondViewController` and we would have solved this bug in 30 seconds. – vacawama Apr 11 '18 at 14:08
  • @KaziAbdullahAlMamun nothing, myBool: Bool() is invalid though. vacamama agreed we would of got to the issue quicker. I use Swiftlint and validate my code style to that. https://github.com/realm/SwiftLint/blob/master/Rules.md#force-unwrapping – Scriptable Apr 11 '18 at 14:12