Search code examples
swiftviewwillappear

Why are interface changes done in viewWillAppear not invisible?


I have a modal ViewController acting as a custom alert in my app.

In it, I have a UIButton title set to some "default text" in Interface Builder.

In my viewWillAppear method, I am setting the button title to "new text".

When I run the app and alert VC appears, I can visibly see the transition of UIButton from "default text" to "new text".

I thought all processing in viewWillAppear() was supposed to be invisible. Any thoughts? (I do not want to make these changes in ViewDidLoad.)


Solution

  • What you can do is create an instance of the class of the viewcontroller (Modal) and set the value of a variable that saves the title and launch that new instance. and in the didload do the assignment

    class ViewControllerOne : UIViewController{
        if let modalVC = self.storyboard?.instantiateViewController(withIdentifier: "modal") as? MYViewController {
            modalVC.titleLbl.text = "New title"    
            self.present(modalVC, animated: true, completion: nil)
        }
    
    }
    
    class MYViewController : UIViewController{      
        @IBOutlet weak var titleLbl: UILabel!
        ...
    }
    

    Sorry my english is not very good