私はUIAlertControllerに関する多くのチュートリアルを探してきました。これまでのところ、私が見つけた方法は、UIAlertControllerをボタンまたはラベルにリンクしてアクティブ化し、IBActionを呼び出すことでした。
ユーザーがアプリに入ったときにアラートを自動的にポップするようにコードを複製しようとしました(チュートリアルを実行するかどうかをユーザーに尋ねたかったのです)。ただし、エラーが発生し続けます。
警告:ビューがウィンドウ階層にないMainViewControllerでUIAlertControllerを提示しようとしています!
次に、addChildViewControllerとaddSubviewを介してUIAlertControllerをMainViewControllerに追加しようとしました。ただし、次のエラーが発生します。
アプリケーションがアクティブなコントローラーをモーダルに提示しようとしました
presentViewController関数が使えないと思い、コメントアウトしました。
UIAlertControllerは表示されますが、キャンセルまたは決してボタンをクリックしようとすると、このエラーが発生します。
プレゼンターが不明なUIAlertControllerを却下しようとしています。
私は本当に困惑しています。誰かが私が間違っていることを共有できますか?どうもありがとうございます。これがコードです。
func displayTutorial() {
alertController = UIAlertController(title: NSLocalizedString("tutorialAlert", comment: ""), message: NSLocalizedString("tutorialMsg", comment: ""), preferredStyle: .ActionSheet)
self.addChildViewController(alertController)
self.view.addSubview(alertController.view)
alertController.didMoveToParentViewController(self)
alertController.view.frame.origin.x = self.view.frame.midX
alertController.view.frame.origin.y = self.view.frame.midY
//alertController.popoverPresentationController?.sourceView = self.view*/
let OkAction = UIAlertAction(title: NSLocalizedString("yesh", comment: ""), style: .Destructive) { (action) in
}
alertController.addAction(OkAction)
let cancelAction = UIAlertAction(title: NSLocalizedString("notNow", comment: ""), style: .Destructive) { (action) in
//println(action)
self.tutorial = 1
self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)
}
alertController.addAction(cancelAction)
let neverAction = UIAlertAction(title: NSLocalizedString("never", comment: ""), style: .Cancel) { (action) in
self.tutorial = 1
}
alertController.addAction(neverAction)
//self.presentViewController(alertController, animated: false) {}
}
私は解決策を見つけました。どうやら、funcviewDidLoadからUIAlertControllerを呼び出すことはできません。viewDidAppearから関数を呼び出す必要があります。だから私のコードは今
override func viewDidAppear(animated: Bool) {
if tutorial == 0 {
displayTutorial(self.view)
}
}
func displayTutorial(sender:AnyObject) {
let alertController = UIAlertController(title: NSLocalizedString("tutorialAlert", comment: ""), message: NSLocalizedString("tutorialMsg", comment: ""), preferredStyle: .ActionSheet)
let OkAction = UIAlertAction(title: NSLocalizedString("yesh", comment: ""), style: .Destructive) { (action) in
}
alertController.addAction(OkAction)
let cancelAction = UIAlertAction(title: NSLocalizedString("notNow", comment: ""), style: .Default) { (action) in
//println(action)
self.tutorial = 1
self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)
}
alertController.addAction(cancelAction)
let neverAction = UIAlertAction(title: NSLocalizedString("never", comment: ""), style: .Cancel) { (action) in
self.tutorial = 1
}
alertController.addAction(neverAction)
self.presentViewController(alertController, animated: true, completion: nil)
if let pop = alertController.popoverPresentationController {
let v = sender as UIView
pop.sourceView = view
pop.sourceRect = v.bounds
}
}
この投稿のおかげで:警告:ビューがウィンドウ階層にない*に*を提示しようとしています-迅速
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加