UIAlert不使用NSNotification

时间:2015-11-07 06:09:13

标签: ios swift 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)        }
}

}

这不起作用,我收到了错误

  

不允许在取消分配时尝试加载视图控制器的视图,并且可能导致未定义的行为

我不明白这是什么意思。 如果我把打印的代码(“无法访问”)工作正常。

我的问题

该错误的含义是什么?

我如何使我的警报工作?

如果有其他方式让用户了解互联网连接,

请告诉我。

1 个答案:

答案 0 :(得分:0)

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