【Xcode 10, swift 4】 UIAlertController を表示してからsegueで画面遷移するコードを書きましたが、エラーメッセージが出て、しばらくつまずきました。
下図のButtonを押すと・・・
以下のようなalertを表示して、キャンセルなら何もしない、OKなら次の画面に遷移するというコードを書きました。Buttonは"myButton"、segue identifierは"toSecondView"としています。
import UIKit
class ViewController: UIViewController {
@IBOutlet var myButton:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func myActionSegue(){
let titleString = "画面遷移"
let messageString = "画面遷移しますか?"
let alert: UIAlertController = UIAlertController(title:titleString,message: messageString,preferredStyle: UIAlertController.Style.alert)
let okAction: UIAlertAction = UIAlertAction(title: "OK",style: UIAlertAction.Style.default,handler:{(action:UIAlertAction!) -> Void in
self.performSegue(withIdentifier: "toSecondView", sender: true)
})
let cancelAction: UIAlertAction = UIAlertAction(title: "キャンセル",style: UIAlertAction.Style.cancel,handler:{(action:UIAlertAction!) -> Void in
})
alert.addAction(okAction)
alert.addAction(cancelAction)
self.view?.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
画面遷移自体はうまく行きましたが、コンソールに以下のようなエラーメッセージが表示されました。
pushViewController:animated: called on <・・・> while an existing transition or presentation is occurring; the navigation stack will not be updated.
しばらく悩みましたが、原因が分かりましたので提示します。
当初、以下の図のように、storyboardでフィールド上の「Button」を右クリックしてsecondViewにpushしていました。これがエラーメッセージの原因でした。
以下の図のように、画面上部の黄色い印を右クリック(矢印)してsecondViewにpushした所、エラーメッセージが消えました。
以上、皆様のご参考になるかと思い、提示させて頂きました。