![](/img/trans.png)
[英]Exception when presenting modal view controller: NSInternalInconsistencyException
[英]UIAlertAction closure is not being called when presenting Controller is modal
我有一個UIViewController
嵌入在UINavigationController
中,它在我的應用程序的keyWindow.rootViewController
上以模態方式呈現。 當我在呈現的導航UIAlertController
的任何屏幕中呈現 UIAlertController 時,警報 controller 會正確顯示,但在按下后不會調用任何UIAlertAction
的關閉。
我在屬於我的應用程序的主導航 controller 的視圖控制器中顯示相同的警報 controller 和相同的代碼,並且正確調用了閉包。
顯示警報的代碼非常簡單,下面是代碼片段:
// Creating Alert
func createAlert() -> UIAlertController {
let actions: [UIAlertAction] = [
UIAlertAction(title: "Something", style: .default, handler: { _ in
self.setOriginalQueue(with: items, description: description, identifier: identifier)
self.setQueue()
}),
UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
normal()
})
]
let alert = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet, actions: actions)
return alert
}
// In ViewController
DispatchQueue.main.async {
self.present(alert, animated: true, completion: nil)
}
所以問題是,為什么不調用閉包?
UIAlertController不包含四種類型參數。 請參閱文檔。
修復后,rest 將自動修復,如下所示。
為了大家方便,我把整個Modal view controller class源碼放在這里。
class ModalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.present(createAlert(), animated: true, completion: nil)
}
func createAlert() -> UIAlertController {
let actions: [UIAlertAction] = [
UIAlertAction(title: "Something", style: .default, handler: { _ in
print("Something")
}),
UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
print("Cancel")
})
]
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
for action in actions {
alert.addAction(action)
}
return alert
}
}
線路錯了。
let alert = UIAlertController(title: nil, message: "message" , preferredStyle: .actionSheet, actions: actions)
你一定會得到這樣的錯誤
Type of expression is ambiguous without more context
像這樣改變它:
return UIAlertController(title: YourTitle, message: YourMessage, preferredStyle: UIAlertController.Style.actionSheet)
所以它可以工作。
呈現警報時需要傳遞方法的名稱
self.present(createAlert(), animated: true, completion: nil)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.