gpt4 book ai didi

ios - UIAlert 不适用于 NSNotification

转载 作者:行者123 更新时间:2023-11-28 08:53:55 26 4
gpt4 key购买 nike

我将 Reachability.swift 文件放在我的应用程序中,当互联网可达性发生变化时,我想提醒用户互联网连接不可用。

这是我的代码。

import UIKit
import Parse

class ViewController: UIViewController {
var reachability : Reachability?
var myAlert = UIAlertController()

@IBOutlet weak var label: UILabel!
@IBOutlet weak var textField: UITextField!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)

do {
let reachability = try Reachability.reachabilityForInternetConnection()
self.reachability = reachability
} catch ReachabilityError.FailedToCreateWithAddress(let address) {

}
catch {}


NSNotificationCenter.defaultCenter().addObserver(self, selector: "HeyUserInternetDoesntWork", name: ReachabilityChangedNotification, object: nil)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func saveButtonTapped(sender: AnyObject) {

let save = PFObject(className: "Practice")
save["text"] = textField.text
save.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
print("Object has been saved.")
}

}


dynamic func HeyUserInternetDoesntWork() {


if reachability!.isReachable() {


} else {


myAlert = UIAlertController(title: "No internet", message: "no good", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction) }
}

这是行不通的,我得到一个错误

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behaviour

我不明白这是什么意思。如果我输入 print("unreachable") 将正常工作的代码。

我的问题

这个错误是什么意思?

我怎样才能让我的警报起作用?

如果有其他方式让用户知道 Internet 连接情况,

请告诉我。

最佳答案

尝试在警报下添加这一行(self.presentViewController(myAlert, animated: true, completion: nil))

关于ios - UIAlert 不适用于 NSNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33579736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com