【问题标题】:Warning when UIAlertController: 0x7ff211650140 called in Swift在 Swift 中调用 UIAlertController: 0x7ff211650140 时发出警告
【发布时间】:2023-03-28 17:25:01
【问题描述】:

我有时会在致电UIAlert 时收到此警告

尝试加载视图控制器的视图 不允许解除分配,并可能导致未定义的行为 ()

code used:

    func alert (dictKey: String){
        print(dictKey)
        let alertController = UIAlertController(title: nil, message: promptsArr[dictKey], preferredStyle: .Alert )
        let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in

        }
        alertController.addAction(OKAction)
        if self.presentedViewController == nil {
            delay(1.0){
                self.presentViewController(alertController, animated: true, completion: nil)
            }
        } else {
            self.presentedViewController!
        }

【问题讨论】:

  • 你在哪里以及如何调用这个方法?
  • 从此开关代码中: func resultOfValidation (dictKey: String, tag: Int) { delay(0.3){ switch dictKey { case "SendVeriCode": //SMS sent //Server code will have to在不同名称下搜索单元格编号的先前条目 _ = self.alert(dictKey) if (tag
  • 请将该代码也添加到问题中。

标签: ios swift uialertcontroller


【解决方案1】:
  1. 删除delay()

  2. 把逻辑放在self.presentedViewController里面: (如果存在则无需创建警报)

    func alert (dictKey: String){
        if self.presentedViewController == nil {
            let alertController = UIAlertController(
                title: nil,
                message: promptsArr[dictKey],
                preferredStyle: .Alert )
            let OKAction = UIAlertAction(title: "OK", style: .Default) {
                (action) in
            }
            alertController.addAction(OKAction)
    
            self.presentViewController(
                alertController,
                animated: true,
                completion: nil)
        }
    }
    

【讨论】:

  • 谢谢你马上试试。
  • 工作得很好,谢谢。 delay(1.0){ } 称为同步两个同时运行的进程的非常有用的方法,也可用于将代码中的 OK 替换为清除警报的特定时间延迟 - 请参阅 /* 由 Matt 创建的方便代码Neuburg,许多书籍的作者,包括 iOS Programming Fundamentals with Swift (O'Reilly 2015)。有关详细信息,请参阅他在 Stack Overflow 中的回复:stackoverflow.com/questions/24034544/… */ 我从哪里得到的
  • stackoverflow.com/questions/24034544/… 与您在这里可能需要的情况截然不同。 delay 绝不是一种可靠的同步方法。查看-performSelector:afterDelay、信号量,或者如果可能的话,查看简单的完成块。
  • 感谢您查看这些建议
  • 感谢您的回答 - 希望我的重新格式化将删除否决票
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 1970-01-01
相关资源
最近更新 更多