如何从 appDelegate 访问每个 ViewController?
How to access every ViewController from appDelegate?
我的问题是我正在使用 Alamofire,我从以下位置添加了 AlamofireActivityIndicator:
AlamofireNetworkActivityIndicator GitHub
我的问题是如何添加一个视图,viewController 用户在那里并进行网络请求,该视图显示指示器而不是在状态栏中显示指示器!
我有问题,我将框架代码更改为:
来自 AlamofireNetworkActivityIndicator 框架的原始代码:
private var activityIndicatorState: ActivityIndicatorState = .notActive {
didSet {
switch activityIndicatorState {
case .notActive:
isNetworkActivityIndicatorVisible = false
invalidateStartDelayTimer()
invalidateCompletionDelayTimer()
case .delayingStart:
scheduleStartDelayTimer()
case .active:
invalidateCompletionDelayTimer()
isNetworkActivityIndicatorVisible = true
case .delayingCompletion:
scheduleCompletionDelayTimer()
}
}
}
我已经添加了两种创建和删除屏幕中心 50*50 红色视图的方法,稍后我将对其进行自定义以显示指示器
fileprivate func myIndicatorView (superView:UIView) {
let view = UIView.init()
view.tag = 2018
view.backgroundColor = .red
superView.addSubview(view)
view.frame = CGRect(x: UIScreen.main.bounds.midX-25,
y: UIScreen.main.bounds.midY-25,
width: 50,
height: 50)
}
fileprivate func removeIndicatorView (superView:UIView) {
superView.subviews.forEach{ (myView) in
if myView.tag == 2018 {
myView.removeFromSuperview()
}
}
}
,但现在我的问题在这里:
private var activityIndicatorState: ActivityIndicatorState = .notActive {
didSet {
switch activityIndicatorState {
case .notActive:
self.myIndicatorView(superView: <#T##UIView#>)
isNetworkActivityIndicatorVisible = false
invalidateStartDelayTimer()
invalidateCompletionDelayTimer()
case .delayingStart:
scheduleStartDelayTimer()
case .active:
self.removeIndicatorView(superView: <#T##UIView#>)
invalidateCompletionDelayTimer()
isNetworkActivityIndicatorVisible = true
case .delayingCompletion:
scheduleCompletionDelayTimer()
}
}
}
问题:
如何定义 ViewControllers 视图而不是 <#T##UIView#> ! ?? ?
这个框架的用法是你在 appDelegate 中输入一行,它观察每个网络请求:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
NetworkActivityIndicatorManager.shared.isEnabled = true
return true
}
你的问题不清楚。但我认为您想从用户进行网络请求的地方获取您的最后一个控制器。
请使用此代码从应用
的任意位置获取最后 viewcontroller
extension UIApplication {
class func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
}
因此,如果您使用的是 CocoaPods,那么您可以在项目中轻松使用 SVProgressHUD 来显示 activity 指示器。检查以下 link
https://github.com/SVProgressHUD/SVProgressHUD
首先,将以下行添加到您的 Podfile 中:
荚'SVProgressHUD'
其次,将 SVProgressHUD 安装到您的项目中:
pod 安装
将以下行添加到您的 Podfile 中:
use_frameworks!
就是这样,现在您可以导入 SVProgressHUD 并使用其方法。
查看下图以供参考。
成功完成上述过程后,只需执行以下操作:
- 导入 SVProgressHUD
- 在进行任何网络调用之前使用类似 SVProgressHUD.show() 的方法
- 在完成回调中使用类似 SVProgressHUD.dismiss() 的方法。
您不应尝试联系特定的控制器,而应使用通知模式广播消息。
添加以下行以发送 "Add Loader Message"
NotificationCenter.default.post(name: Notification.Name(rawValue: "AddLoader", object: nil)
添加以下内容以关闭:
NotificationCenter.default.post(name: Notification.Name(rawValue: "HideLoader", object: nil)
现在在每个控制器中(如果你有 Super
视图控制器就更容易了):
- 在视图中会出现:
NotificationCenter.default.addObserver(self, selector: #selector(yourShowSelector), name: Notification.Name(rawValue: "AddLoader"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(yourHide
Selector), name: NSNotification.Name(rawValue: "HideLoader"), object: nil)
在视图中会消失
NotificationCenter.default.removeObserver(self)
我的问题是我正在使用 Alamofire,我从以下位置添加了 AlamofireActivityIndicator: AlamofireNetworkActivityIndicator GitHub
我的问题是如何添加一个视图,viewController 用户在那里并进行网络请求,该视图显示指示器而不是在状态栏中显示指示器!
我有问题,我将框架代码更改为:
来自 AlamofireNetworkActivityIndicator 框架的原始代码:
private var activityIndicatorState: ActivityIndicatorState = .notActive {
didSet {
switch activityIndicatorState {
case .notActive:
isNetworkActivityIndicatorVisible = false
invalidateStartDelayTimer()
invalidateCompletionDelayTimer()
case .delayingStart:
scheduleStartDelayTimer()
case .active:
invalidateCompletionDelayTimer()
isNetworkActivityIndicatorVisible = true
case .delayingCompletion:
scheduleCompletionDelayTimer()
}
}
}
我已经添加了两种创建和删除屏幕中心 50*50 红色视图的方法,稍后我将对其进行自定义以显示指示器
fileprivate func myIndicatorView (superView:UIView) {
let view = UIView.init()
view.tag = 2018
view.backgroundColor = .red
superView.addSubview(view)
view.frame = CGRect(x: UIScreen.main.bounds.midX-25,
y: UIScreen.main.bounds.midY-25,
width: 50,
height: 50)
}
fileprivate func removeIndicatorView (superView:UIView) {
superView.subviews.forEach{ (myView) in
if myView.tag == 2018 {
myView.removeFromSuperview()
}
}
}
,但现在我的问题在这里:
private var activityIndicatorState: ActivityIndicatorState = .notActive {
didSet {
switch activityIndicatorState {
case .notActive:
self.myIndicatorView(superView: <#T##UIView#>)
isNetworkActivityIndicatorVisible = false
invalidateStartDelayTimer()
invalidateCompletionDelayTimer()
case .delayingStart:
scheduleStartDelayTimer()
case .active:
self.removeIndicatorView(superView: <#T##UIView#>)
invalidateCompletionDelayTimer()
isNetworkActivityIndicatorVisible = true
case .delayingCompletion:
scheduleCompletionDelayTimer()
}
}
}
问题:
如何定义 ViewControllers 视图而不是 <#T##UIView#> ! ?? ? 这个框架的用法是你在 appDelegate 中输入一行,它观察每个网络请求:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
NetworkActivityIndicatorManager.shared.isEnabled = true
return true
}
你的问题不清楚。但我认为您想从用户进行网络请求的地方获取您的最后一个控制器。
请使用此代码从应用
的任意位置获取最后 viewcontrollerextension UIApplication {
class func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
}
因此,如果您使用的是 CocoaPods,那么您可以在项目中轻松使用 SVProgressHUD 来显示 activity 指示器。检查以下 link
https://github.com/SVProgressHUD/SVProgressHUD
首先,将以下行添加到您的 Podfile 中:
荚'SVProgressHUD'
其次,将 SVProgressHUD 安装到您的项目中:
pod 安装
将以下行添加到您的 Podfile 中:
use_frameworks!
就是这样,现在您可以导入 SVProgressHUD 并使用其方法。
查看下图以供参考。
成功完成上述过程后,只需执行以下操作:
- 导入 SVProgressHUD
- 在进行任何网络调用之前使用类似 SVProgressHUD.show() 的方法
- 在完成回调中使用类似 SVProgressHUD.dismiss() 的方法。
您不应尝试联系特定的控制器,而应使用通知模式广播消息。
添加以下行以发送 "Add Loader Message"
NotificationCenter.default.post(name: Notification.Name(rawValue: "AddLoader", object: nil)
添加以下内容以关闭:
NotificationCenter.default.post(name: Notification.Name(rawValue: "HideLoader", object: nil)
现在在每个控制器中(如果你有 Super
视图控制器就更容易了):
- 在视图中会出现:
NotificationCenter.default.addObserver(self, selector: #selector(yourShowSelector), name: Notification.Name(rawValue: "AddLoader"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(yourHide
Selector), name: NSNotification.Name(rawValue: "HideLoader"), object: nil)
在视图中会消失
NotificationCenter.default.removeObserver(self)