警告 - 尝试在 Swift 中呈现 UIAlertController null
Posted
技术标签:
【中文标题】警告 - 尝试在 Swift 中呈现 UIAlertController null【英文标题】:Warning - Attempt to present UIAlertController null in Swift 【发布时间】:2016-10-03 09:09:05 【问题描述】:我有一个 UIAlertController 在我的项目中运行,它通过 UIButton 连接。我的代码工作正常。但是,每次当我按下按钮时,Xcode 控制台都会打印如下所示的警报,并且我经历了在 *** 中提出的所有类似问题但是没有答案。Attempt to present UIAlertController on UIViewController whose view is not in the window hierarchy
2016-10-03 19:14:35.854 xxxxxx[85186:7214348] Warning: Attempt to present<UIAlertController: 0x7f8d99450820> on <yyyyyyy.ViewController: 0x7f8d9a025200> which is already presenting (null)
下面是我的代码:
override func viewDidLoad()
super.viewDidLoad()
self.moreButton.frame = CGRect(x:view.frame.width / 2, y: view.frame.height / 2, width: 100, height: 40)
moreButton.center = view.center
moreButton.backgroundColor = UIColor.darkText.withAlphaComponent(0.4)
moreButton.layer.masksToBounds = true
moreButton.setTitle("More", for: UIControlState())
moreButton.setTitleColor(UIColor.white, for: UIControlState())
moreButton.showsTouchWhenHighlighted = true
moreButton.layer.cornerRadius = 5.0
moreButton.addTarget(self, action: #selector(ViewController.moreButtonAction(_:)), for: .allTouchEvents)
self.view.addSubview(self.moreButton)
func moreButtonAction(_ sender: UIButton)
DispatchQueue.main.async(execute:
let myAlert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
myAlert.view.tintColor = UIColor.purple.withAlphaComponent(0.8)
let myAction_1 = UIAlertAction(title: " Title 1", style: UIAlertActionStyle.default, handler:
(action: UIAlertAction!) in
)
let myAction_2 = UIAlertAction(title: " Title 2", style: UIAlertActionStyle.default, handler:
(action: UIAlertAction!) in
)
let myAction_3 = UIAlertAction(title: " Title 3", style: UIAlertActionStyle.default, handler:
(action: UIAlertAction!) in
)
let myAction_4 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler:
(action: UIAlertAction!) in
)
myAlert.addAction(myAction_1)
myAlert.addAction(myAction_2)
myAlert.addAction(myAction_3)
myAlert.addAction(myAction_4)
self.present(myAlert, animated: true, completion: nil)
)
提前致谢
【问题讨论】:
向我们展示展示警报控制器的代码。 @ozgur 我刚刚上传了我的代码....谢谢 【参考方案1】:错误是告诉您您正在尝试呈现一个已经活动的alertController
。您可以检查您的控制器是否有活动警报,否则会显示它。这将删除警告。所以基本上用以下内容移动你的self.present row
:
if !(self.navigationController?.visibleViewController?.isKind(of: UIAlertController.self))!
self.present(myAlert, animated: true, completion: nil)
将您的 moreActionButton
函数替换为以下内容:
func moreButtonAction(_ sender: UIButton)
let myAlert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
myAlert.view.tintColor = UIColor.purple.withAlphaComponent(0.8)
let myAction_1 = UIAlertAction(title: " Title 1", style: UIAlertActionStyle.default, handler:
(action: UIAlertAction!) in
)
let myAction_2 = UIAlertAction(title: " Title 2", style: UIAlertActionStyle.default, handler:
(action: UIAlertAction!) in
)
let myAction_3 = UIAlertAction(title: " Title 3", style: UIAlertActionStyle.default, handler:
(action: UIAlertAction!) in
print("3")
)
let myAction_4 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler:
(action: UIAlertAction!) in
)
myAlert.addAction(myAction_1)
myAlert.addAction(myAction_2)
myAlert.addAction(myAction_3)
myAlert.addAction(myAction_4)
if !(self.navigationController?.visibleViewController?.isKind(of: UIAlertController.self))!
self.present(myAlert, animated: true, completion: nil)
【讨论】:
我把这段代码放在哪里。我在我的 alertView 中尝试过,但我遇到了崩溃...谢谢 @Joe,检查更新。因此,只需将您的 moreActionButton 函数替换为我在答案中添加的函数即可。 。感谢您的代码有效。再次感谢您的努力。以上是关于警告 - 尝试在 Swift 中呈现 UIAlertController null的主要内容,如果未能解决你的问题,请参考以下文章
Swift - 警告提示框(UIAlertController)的用法
尝试呈现其视图不在窗口层次结构中的 UIAlertController (Swift 3/Xcode 8)
警告:尝试呈现不在窗口层次结构中的视图!在 UISearchController
警告:尝试在视图不在窗口层次结构中的 **.ViewController 上呈现 ZMNavigationController