UIAlert 不适用于 NSNotification
UIAlert does not working with NSNotification
我在我的应用程序中放置了 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))
我在我的应用程序中放置了 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))