1
votes

Say, I have a label show : Loading...

problem: When return from VC(2). The label is not hidden.

How to hide it when return from VC(2) and dont hide it when in navigating to VC(2) and show the message : Loading....


in VC(1) 

 @IBOutlet weak var lbLoadingMsg

 In viewDidLoad() {

 lbLoadingMsg.hidden = true

}  


-2-- turn it on when prepare to navigate to VC(2)


override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool
{
  --code--

   lbLoadingMsg.hidden = false

}


Override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){

}

3

3 Answers

1
votes

You can use NSNotificationCenter for that.

Follow this simple steps:

1.In your VC(2) add this code into your button from where you are going back:

@IBAction func goBack(sender: AnyObject) {

    NSNotificationCenter.defaultCenter().postNotificationName("hide", object: nil)
    self.dismissViewControllerAnimated(true, completion: nil)
}

2.In your First View add this code into viewDidLoad method:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideLabel:", name:"hide", object: nil)
}

now this method will call this function:

func hideLabel(notification: NSNotification){

    self.lbLoadingMsg.hidden = true
}

And this will hide your label in first view when ever goBack button will pressed from first view.

Hope this will help you.

0
votes

Write this in VC2 ,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var identifier = segue.identifier

    if(identifier! == "yourIdentifier"){
        var vc1:VC1 = segue.destinationViewController as! VC1

        vc1.lbLoadingMsg.hidden = true
    }
}
0
votes
func viewDidAppear(_ animated: Bool) {
  lbLoadingMsg.hidden = true
}

Move

lbLoadingMsg.hidden = true

line from viewDidLoad to viewDidAppear. I think most quicker way.