Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

segue

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.

like image 567
mazy Avatar asked Dec 11 '25 13:12

mazy


2 Answers

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.

like image 151
imbbTG Avatar answered Dec 14 '25 03:12

imbbTG


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)
}
like image 26
AlexanderZ Avatar answered Dec 14 '25 02:12

AlexanderZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!