2

When print(mess) is called from the viewDidLoad function, it prints what is expected, but when called from the viewWillAppear function, it gives empty output. label also not getting updated after doing the assignment in viewDidLoad .Why is this?

main ViewController:

if segue.identifier == "temp"
{
let tovc = segue.destination as ! ADDviewcontroller1
tovc.mess = self.var1
}

In ADDviewcontroller1:

var mess: String = ""
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
self.infoLabel.text = mess
print(mess) //giving empty output in console
}

override func viewDidLoad()
{
super.viewDidLoad()
self.infoLabel.text = mess //label not getting updated

print(mess) //printing the required output in console
}
san
  • 29
  • 6
  • Maybe someone else changes the variable value. – Cristik Mar 07 '18 at 12:17
  • you mean in main ViewController tovc.mess = self.var1 – Vasilis D. Mar 07 '18 at 12:17
  • 3
    Are you sure `mess` is nil? Because in Swift non optional variables cant be assigned nil. So something really strange is happening or you haven't been descriptive enough about the problem you're facing. – Alex Mar 07 '18 at 12:36
  • Please add some hint to the print, to check from where the nil is coming, e.g. print("from viewWillAppear", mess) and print("from viewDidLoad", mess). Because I don't think the nil is coming from any of these. mess is initialized as empty string and not as optional. – mahega Mar 07 '18 at 14:02
  • @mh-itc done so before posting this question ..not included that over here – san Mar 07 '18 at 14:36
  • @Alex sorry! my mistake it is giving empty output in viewWillAppear . Thanks for pointing out. I have edited my post – san Mar 08 '18 at 04:33

2 Answers2

0

I have reproduced your code and found several mistakes, I have a working example below and think I have found your issue.

First viewcontroller (ViewController)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "show" {
        let tovc = segue.destination as? AddViewController
        tovc?.mess = "test"
    }
}

Note: I have renamed the identifier to show, rename it back to whatever it is at your code. Also I used the string "test" as a replacement for your var1

In your first viewcontroller you will have to overwrite the prepare(for:sender:) func with the code you provided. Note that the ! was one space away from the as statement. I concatenated them and changed it into a conditional cast with ?. This way it's more safe.

Second viewcontroller (AddViewController)

@IBOutlet weak var infoLabel: UILabel!

var mess: String = ""
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.infoLabel.text = mess
    print(mess) //this is now printing 'test'
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.infoLabel.text = mess //label is now being set

    print(mess) //this is now printing 'test'
}

Note the extra _ you were missing in the viewWillAppear(animated:) func signature. Not cohering to the signature makes it possible to not have it fired like you expect (because the system won't recognize the viewWillAppear as a valid override).

Running this code in the latest Xcode (9.2) using Swift 4 I have no problems and can't reproduce your issue (if it ain't the missed signature of the viewWillAppear). I'm guessing this issue is to locale if you still run into troubles.

Alex
  • 838
  • 1
  • 14
  • 16
  • I really appreciate your effort @Alex , but still facing the same issue ..getting test printed in viewDidLoad but not in viewWillAppear – san Mar 08 '18 at 10:12
  • Hmm, really strange... Could you possibly share a [SSCCE](http://www.sscce.org/) of the issue, maybe if we can reproduce it we can solve it. – Alex Mar 08 '18 at 12:35
-1

It's impossible for a non-optional value to produce 'nil', but it may be a bug in Xcode try:

1- Clear derived data folder: https://stackoverflow.com/a/18933382/3882414

2- Restart Xcode

3- Clean and rebuild

rami
  • 129
  • 2
  • 10