gpt4 book ai didi

ios - 如果已连接互联网,我如何关闭 View Controller

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

如果连接到互联网,我试图关闭 View Controller 。当没有互联网时,它会显示我的 noInternetViewController 但是当我重新连接到互联网时,noInternetViewController View 并没有被关闭。

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)
}
}

}
}

和 NoInternetViewController:

import UIKit

class NoInternetViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
}
}

谢谢你的帮助

最佳答案

在您的 NoInternetViewController 中,您也可以向其添加一个 Observer。当您收到连接时,发布通知以触发其选择器自行关闭。

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)
}

关于ios - 如果已连接互联网,我如何关闭 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41076381/

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