3

Okey, I have been struggling this for a long time now.. Cant get it to work, and I know there is an easy solution that I cannot find.

I have a UIViewController displaying the main app. When it launches there is two containerViews overlaid that displays a count down for the app to start. Countdown is done, and the "overlay" viewcontrollers are removed from superview.

BUT, I need to call the function to remove VC from superview and start the app in the main UIViewController. But I am not able to trigger this function from the separate "overlay" UIView..

If I make an instance of the UIViewController to trigger the startGame() function it works, but then all the labels return nil, and the app crashes.. I believe this is because the UIViewController is triggered twice?

A lot of explaining here.. I will try to show some outtakes in code:

Main UIViewController:

class DMStartVC: UIViewController {
.....
func startGame() {
    self.view.viewWithTag(100)?.removeFromSuperview()
    self.view.viewWithTag(101)?.removeFromSuperview()
    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(DMStartVC.update), userInfo: nil, repeats: true)
    UITimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(DMStartVC.UIUpdate), userInfo: nil, repeats: true)
  }
....
}

The UIView handling the "overlay" container views:

class countdown: UIView {
  let dmStartVC = DMStartVC()
  ...
  //when animation is done:
  dmStartVC.startGame()
}

This causes crash because all labels in the UIViewController is nil. How can I reach that .startGame() function?

Only way I have been able to solve this is adding a timer inside the DMStartVC to trigger .startGame()... But that is not a good solution as the timer has to trigger at the exact time the countdown ends.. and it never does.

Frank
  • 61
  • 1
  • 4
  • 1
    this might help you: http://stackoverflow.com/questions/30541010/how-to-reload-data-in-a-tableview-from-a-different-viewcontroller-in-swift – Justin Doan Oct 17 '16 at 18:35
  • @JustinDoan You are a life saver! Thank you so much, this solved the problem in a much smoother way :) – Frank Oct 17 '16 at 18:41

0 Answers0