Swift:循环时内部的UialertController



在以下代码上挣扎:

 var connected = false
    while !connected {
        let msg = "Press cancel"
        let alert = UIAlertController(title: "Test", message: msg, preferredStyle: .alert)
        let action = UIAlertAction(title: "Cancel", style: .default) { (action:UIAlertAction) in
            connected = true
        }
        alert.addAction(action)
        print ("Hello")
        present(alert, animated: true, completion: nil)
    }
  1. uialertController从未显示出"你好"一遍又一遍地打印
  2. 如果我在子句之后插入"连接= true",则显示uialertController,但我无法通过将操作更改为"连接= false"
  3. 来再次显示它

我在做什么错?

首先,如评论中所述,在一段时间内连续呈现警报控制器并不是一个好主意。我相信您的预期功能是在connected变量变为false时显示警报。

完成此使用NotificationCenter以如下响应:

viewDidLoad中:

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.displayAlert), name: NSNotification.Name(rawValue: "connectionDropped"), object: nil)

willSet属性观察者添加到connected

var connected: Bool! {
  willSet {
    if newValue == false && oldValue != false {
      NotificationCenter.default.post(name: NSNotification.Name(rawValue: "connectionDropped"), object: nil)
    }
  }
}

然后,每当您设置self.connected = false时,都将运行此方法:

@objc func displayAlert() {
  let msg = "Press cancel"
  let alert = UIAlertController(title: "Test", message: msg, preferredStyle: .alert)
  let action = UIAlertAction(title: "Cancel", style: .default) { (action:UIAlertAction) in
    self.connected = true
  }
  alert.addAction(action)
  print ("Hello")
  present(alert, animated: true, completion: nil)
}

只需确保在视图层次结构被加载之后设置了连接,例如viewDidAppear

完成视图后,您可以删除观察者:

deinit {
  NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "connectionDropped"), object: nil)
}

编辑:

您需要的功能与可及性框架一起提供,尤其是reachabilityChanged通知。然后,您可以使用我上面概述的类似方法调用displayAlert;这是在他们的读书文档中记录的。

  1. uialertController从未显示出"你好"一遍又一遍地打印

不合适的继续以无尽的while循环显示警报,这可能导致面对" AlertController不在窗口层次结构"问题。

  1. 如果我在子句之后插入"连接= true",则显示uialertController,但我无法通过 将动作更改为"连接= false"

当然,默认情况下您将无法再次显示它,代码的一部分已被执行。

解决方案:

呈现警报应与触发特定条件触发条件有关,而不是在 while循环中。

例如,如果您的目标是通知用户该设备没有连接,则可以使用可及性并观察reachabilityChanged来显示警报,有关更多详细信息,您可以查看库 - 示例 - 示例-Notifications 分。

我设法使用对该功能的递归调用来解决它:

    func showDlg(){
    ssid = getWiFiSsid() ?? "AAA"
    ssid = ssid[ssid.startIndex..<ssid.index(ssid.startIndex, offsetBy: 2)]
    if ssid != "GP" {
        print ("SSID: " + ssid)
        let msg = "nNot connected to GoPro camerannPlease connect to camera wireless network and revertn "
        alert = UIAlertController(title: "GoPro Golfswing Analyser", message: msg, preferredStyle: .alert)
        let action = UIAlertAction(title: "Try again", style: .default) { (action:UIAlertAction) in
            print("Testing network again")
            self.showDlg()
        }
        alert.addAction(action)
        let action1 = UIAlertAction(title: "Cancel", style: .default) { (action:UIAlertAction) in
            print("Cancel - exiting")
        }
        alert.addAction(action1)
        present(alert, animated: true, completion: nil)
    } else{
        print("Connected to GoPro")
    }
}

相关内容

  • 没有找到相关文章

最新更新