mazy
mazy

Reputation: 690

Swift segue - Presenting view controllers on detached view controllers is discouraged

I want to open "Select city" controller, when selectedCityId is 0 (Undefined) - to select a city. In my root view controller (MainVC) in viewDidLoad():

let selectedCityId = NSUserDefaults.standardUserDefaults().integerForKey("selectedCityId")

    if(selectedCityId == 0){
        self.performSegueWithIdentifier("startupSegue", sender: self)
    } else {
        ....
    }

So the startupSegue opens settingsViewController (SettingsVC) modally! Both - MainVC and SettingsVC have embed in Navigation View Controller.

There are 2 warnings:

Presenting view controllers on detached view controllers is discouraged "myapp.MainVC: 0x7fee8871e670".

Unbalanced calls to begin/end appearance transitions for "UINavigationController: 0x7fee8872c950".

Thanks for answers.

Upvotes: 3

Views: 4582

Answers (2)

AlexanderZ
AlexanderZ

Reputation: 2158

Moving code to -viewDidAppear is a good idea but I would also suggest to perform your seque call from the main thread:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
  self.performSegueWithIdentifier("startupSegue", sender: self)
})

Swift 3

DispatchQueue.main.async
  {
    self.performSegue(withIdentifier: "startupSegue", sender: self)
}

Upvotes: 2

imbbTG
imbbTG

Reputation: 81

I had a same warning and fixed it by moving code to -viewDidAppear. One thing you should be aware is that -viewDidLoad is called only once while -viewDidAppear maybe called multiple times if page becomes visible so you need to add additional mechanism to skip performing segue again.

Upvotes: 5

Related Questions