获取 App delegate 中 UIVIewController 的名称 swift
Getting the name of UIVIewController in App delegate swift
目前我实现了 Reachability is AppDelegate.swift 文件。这是我确定何时连接或断开互联网的代码
@objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
if (reachability.connection != .none) {
}
else
{
currentView()
}
}
此代码可以很好地检测互联网是否已连接或断开。
现在,如果互联网断开连接,我想知道我目前在 navigationController 中的哪个 viewController 并且我想修改那个 viewController 的 UI 以通知用户互联网是断开连接。我正在尝试使用以下代码在我的 navigationStack 中获取当前 viewController 但它不起作用
if let window = UIApplication.shared.delegate?.window {
if var viewController = window?.rootViewController {
// handle navigation controllers
if(viewController is UINavigationController){
viewController = (viewController as! UINavigationController).visibleViewController!
}
print (viewController)
}
}
我建议您使用 NotificationCenter 并向观察者发送通知,以便在屏幕上显示观察者时执行您想要的操作。让您的 VC 成为观察者并在发送通知时实施选择器。
如果您不知道如何检查视图是否在屏幕上,请检查此答案:
How to tell if UIViewController's view is visible
目前我实现了 Reachability is AppDelegate.swift 文件。这是我确定何时连接或断开互联网的代码
@objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
if (reachability.connection != .none) {
}
else
{
currentView()
}
}
此代码可以很好地检测互联网是否已连接或断开。 现在,如果互联网断开连接,我想知道我目前在 navigationController 中的哪个 viewController 并且我想修改那个 viewController 的 UI 以通知用户互联网是断开连接。我正在尝试使用以下代码在我的 navigationStack 中获取当前 viewController 但它不起作用
if let window = UIApplication.shared.delegate?.window {
if var viewController = window?.rootViewController {
// handle navigation controllers
if(viewController is UINavigationController){
viewController = (viewController as! UINavigationController).visibleViewController!
}
print (viewController)
}
}
我建议您使用 NotificationCenter 并向观察者发送通知,以便在屏幕上显示观察者时执行您想要的操作。让您的 VC 成为观察者并在发送通知时实施选择器。
如果您不知道如何检查视图是否在屏幕上,请检查此答案: How to tell if UIViewController's view is visible