如果已连接互联网,我如何关闭视图控制器
How can i dismiss view controller if internet connected
如果已连接互联网,我将尝试关闭视图控制器。当没有互联网时它会显示我的 noInternetViewController 但是当我重新连接到互联网时,noInternetViewController 视图并没有被关闭。
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: ReachabilityChangedNotification, object: reachability)
}
func reachabilityChanged(note: NSNotification) {
let reachability = note.object as! Reachability
if reachability.isReachable {
if noInternet == true {
DispatchQueue.main.async {
self.noInternet = false
self.dismiss(animated: true, completion: nil)
}
}
} else {
noInternet = true
if noInternet == true {
DispatchQueue.main.async {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let noInternetViewController = storyboard.instantiateViewController(withIdentifier: "NoInternetViewController") as! NoInternetViewController
noInternetViewController.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
self.present(noInternetViewController, animated: true, completion: nil)
}
}
}
}
并且没有 InternetViewController:
import UIKit
class NoInternetViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
感谢您的帮助
在您的 NoInternetViewController
中,您也可以添加一个 Observer
。当您收到连接时,post 触发它的选择器自行关闭的通知。
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(receivedConnection), name: NSNotification.Name.init(rawValue: "ReceivedConnection"), object: nil)
}
func receivedConnection() {
self.dismiss(animated: true, completion: nil)
}
如果已连接互联网,我将尝试关闭视图控制器。当没有互联网时它会显示我的 noInternetViewController 但是当我重新连接到互联网时,noInternetViewController 视图并没有被关闭。
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: ReachabilityChangedNotification, object: reachability)
}
func reachabilityChanged(note: NSNotification) {
let reachability = note.object as! Reachability
if reachability.isReachable {
if noInternet == true {
DispatchQueue.main.async {
self.noInternet = false
self.dismiss(animated: true, completion: nil)
}
}
} else {
noInternet = true
if noInternet == true {
DispatchQueue.main.async {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let noInternetViewController = storyboard.instantiateViewController(withIdentifier: "NoInternetViewController") as! NoInternetViewController
noInternetViewController.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
self.present(noInternetViewController, animated: true, completion: nil)
}
}
}
}
并且没有 InternetViewController:
import UIKit
class NoInternetViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
感谢您的帮助
在您的 NoInternetViewController
中,您也可以添加一个 Observer
。当您收到连接时,post 触发它的选择器自行关闭的通知。
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(receivedConnection), name: NSNotification.Name.init(rawValue: "ReceivedConnection"), object: nil)
}
func receivedConnection() {
self.dismiss(animated: true, completion: nil)
}