[英]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來顯示活動指示器。 檢查以下鏈接
https://github.com/SVProgressHUD/SVProgressHUD
首先,將以下行添加到您的Podfile中:
莢'SVProgressHUD'
其次,將SVProgressHUD安裝到您的項目中:
吊艙安裝
將以下行添加到您的Podfile中:
use_frameworks!
就是這樣,現在您可以使用其方法導入SVProgressHUD。
檢查下面的圖像以供參考。
成功完成上述過程后,只需執行以下操作:
而不是嘗試到達特定的控制器,您應該使用“通知模式”廣播一條消息。
添加以下行以發送“添加加載程序消息”
NotificationCenter.default.post(name: Notification.Name(rawValue: "AddLoader", object: nil)
添加以下內容以撤消:
NotificationCenter.default.post(name: Notification.Name(rawValue: "HideLoader", object: nil)
現在,在每個控制器中(如果您擁有Super
View控制器,則會更輕松):
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.