0

I am trying to execute a function after the view has appeared.

I have tried series of steps I tried wait()

override func viewDidLoad()
{
    super.viewDidLoad()

    let w = UnsafeMutablePointer<Int32>.allocate(capacity: 20)
    self.title = "Quiz Section"
    wait(w)
    Function()
}

I tried sleep()

override func viewDidLoad()
{
    super.viewDidLoad()

    sleep(10)
    Function()
}

I even tried viewWillAppear but none of them seemed to work. I want that once the view is in front then the function executes. Any help?

2
  • 1
    Have you tried viewDidAppear? Commented May 3, 2018 at 20:17
  • 1
    NEVER use any kind of "sleep" on the main queue. Very, very bad. Commented May 3, 2018 at 21:25

2 Answers 2

2

You can try viewDidAppear

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    function()    // don't start function name with capital 
 }

OR dispatch the function in after queue if you need more duration

override func viewDidLoad()
{
    super.viewDidLoad()

    let w = UnsafeMutablePointer<Int32>.allocate(capacity: 20)
    self.title = "Quiz Section"

     DispatchQueue.main.asyncAfter(deadline: .now() + 2.0 ) {
          function()  
     }

}
Sign up to request clarification or add additional context in comments.

2 Comments

If this code is in viewDidAppear, there is no point at all to using asyncAfter.
@rmaddy it's another option not both together
0

Try this:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    function()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.