OStack程序员社区-中国程序员成长平台

标题: ios - 在另一个 Controller 上显示一个按钮 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:55
标题: ios - 在另一个 Controller 上显示一个按钮

我有两个 Controller ,一个 Controller 是 controllerOne.swift,在这个 Controller 中我会收到通知,当一个通知到达时,我需要在 controllerTwo.swift 上显示一个按钮。

我的代码是:

ControllerOne.swift

public func websocket(token: Any){
        self.ws.open("ws://"+String(APIHOST)+":"+String(port)+"/ws?token="+String(describing: token))
        self.ws.event.message = { message in
            let res = self.convertToDictionary(text: message as! String)

            if ((res!["notification"]) != nil) {
                self.count_total_notifications_ws = self.count_total_notifications_ws! + 1
                let presentView = UIApplication.shared.keyWindow?.rootViewController?.presentedViewController as? SWRevealViewController

                let tabbarController = presentView?.frontViewController as? UITabBarController

                if (tabbarController?.selectedIndex != 0) {
                    tabbarController?.tabBar.items?[0].badgeValue = self.count_total_notifications_ws?.description
                }else{
                    //Here I need to show a showNotificationsbtn button
                }
            }
        }
    }

ControllerTwo.swift

class NewDashboardViewController: UIViewController, UITableViewDataSource, UITabBarControllerDelegate, UITableViewDelegate {

    //This is the button that I need show
    @IBOutlet weak var showNotificationsbtn: UIButton!  

    @IBAction func showNotifications(_ sender: Any) {true
        self.viewDidAppear(true)
        showNotificationsbtn.isHidden = true
    }

}

有人知道我该怎么做吗? 感谢您的帮助。



Best Answer-推荐答案


在 ViewControllerOne 中

  if ((res!["notification"]) != nil) {
                self.count_total_notifications_ws = self.count_total_notifications_ws! + 1
                let presentView = UIApplication.shared.keyWindow?.rootViewController?.presentedViewController as? SWRevealViewController

                let tabbarController = presentView?.frontViewController as? UITabBarController

                if (tabbarController?.selectedIndex != 0) {
                    tabbarController?.tabBar.items?[0].badgeValue = self.count_total_notifications_ws?.description
                }else{
                    //Here I need to show a showNotificationsbtn button
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "remoNotificationArrived"), object: nil, userInfo: nil )
                }
            }

在 ViewControllerTwo 中

     override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(true)
            DispatchQueue.main.async {

                NotificationCenter.default.addObserver(self, selector: #selector(self.showButton), name: NSNotification.Name(rawValue: "remoNotificationArrived"), object: nil)

            }
    }

  func showButton(){
       showNotificationsbtn.isHidden = false
    }

关于ios - 在另一个 Controller 上显示一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48378264/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4