无法快速更新“UIView”的服务调用

问题描述 投票:0回答:0

我为所有屏幕添加了全局“UIView”底部标签栏

bottom view with message o/p

showCountServiceCall 是消息计数更新服务调用,当我们读取或获取消息时此方法将调用并更新

在这个底部选项卡中,当我阅读未读消息时,我调用了

showCountServiceCall
来更新消息计数,然后它必须更新。但是当我在 MessageDetailsVC 中阅读消息时,这并没有发生,为什么?但是当我关闭应用程序并运行agin

时它会更新

注意: 我也在 MessageDetailsVC 中调用了

showCountServiceCall
方法,所以当我阅读我的所有消息时,它必须在
tabbar
中更新它,但它没有更新为什么?

代码: TabBarView中的代码

class TabBarView : UIView{

@IBOutlet weak var msgcountLabel: UILabel!

@IBOutlet var viewCollection : [UIView]!
@IBOutlet var viewForImageBacgroundCollection : [UIView]!
@IBOutlet var imgCollection : [UIImageView]!
@IBOutlet var btnCollection : [UIButton]!

override func layoutSubviews() {
    super.layoutSubviews()
    
    msgcountLabel.isHidden = true
    showCountServiceCall()
    NotificationCenter.default.addObserver(self, selector: #selector(updateCount), name: NSNotification.Name(rawValue: updateMessageCountName), object: nil)
    selectedTag = profileIsComplete ? 2: 10
    msgcountLabel.layer.cornerRadius = 10
    msgcountLabel.layer.masksToBounds = true
}

deinit {
    NotificationCenter.default.removeObserver(self)
}

@objc func updateCount() {
    showCountServiceCall()
}

func showCountServiceCall() {
    APIReqeustManager.sharedInstance.serviceCall(param: nil, method: .get,, url: CommonUrl.show_count) { (resp) in
        if let result = resp.dict?["result"] as? NSDictionary {
            let messageCount = result["message_count"] as? Int ?? 0 //Int.random(in: 1...19)
            
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                self.msgcountLabel.text = "\(messageCount)"
                self.msgcountLabel.isHidden = messageCount == 0
            }
        }
    }
}
}

** MessageDetailViewController 中的代码 **

class MessageDetailViewController: UIViewController {

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    NotificationCenter.default.addObserver(self, selector: #selector(didGetNewMessage), name: NSNotification.Name(rawValue: updateMessageCountName), object: nil)
    
    messageDetailsService()
    showCountServiceCall()
}

override func viewDidAppear(_ animated: Bool) {
    
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(applicationWillEnterForeground(_:)),
        name: UIApplication.willEnterForegroundNotification,
        object: nil)
    
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self)
}
@objc func applicationWillEnterForeground(_ notification: NSNotification) {
    showCountServiceCall()
}

deinit {
    
    NotificationCenter.default.removeObserver(self)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(updateMessageCountName), object: nil)
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    
    NotificationCenter.default.removeObserver(self)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(updateMessageCountName), object: nil)
}

func showCountServiceCall() {
    APIReqeustManager.sharedInstance.serviceCall(param: nil, method: .get, loaderNeed: false, loadingButton: nil, needViewHideShowAfterLoading: nil, vc: self, url: CommonUrl.show_count, isTokenNeeded: true, isErrorAlertNeeded: true, isSuccessAlertNeeded: false, actionErrorOrSuccess: nil, fromLoginPageCallBack: nil) { (resp) in
        if let result = resp.dict?["result"] as? NSDictionary {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
             
            }
        }
    }
}
}

请指导我解决问题

swift web-services uiview auto-update
© www.soinside.com 2019 - 2024. All rights reserved.