【问题标题】:Show a button on another controller在另一个控制器上显示一个按钮
【发布时间】:2018-01-22 09:26:06
【问题描述】:

我有两个控制器,一个控制器是 controllerOne.swift,在这个控制器中我收到通知,我需要当一个通知到达时,在 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
    }

}

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

【问题讨论】:

  • 你能详细说明你说的是哪个按钮,是ControllerOne还是ControllerTwo?
  • 对,就是controllerTwo的showNotificationsbtn按钮,就是我要显示的那个

标签: ios xcode swift3


【解决方案1】:

在 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
    }

【讨论】:

  • 你成就了我的一天!这完美地工作。非常感谢。
【解决方案2】:

首先隐藏您的按钮。 现在要取消隐藏该按钮,您有多种选择。 1.使用delgate/protocol在viewcontrollers之间进行通信 2. 可以添加观察者

【讨论】:

    猜你喜欢
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多