ios - 在另一个 Controller 上显示一个按钮
<p><p>我有两个 Controller ,一个 Controller 是 controllerOne.swift,在这个 Controller 中我会收到通知,当一个通知到达时,我需要在 controllerTwo.swift 上显示一个按钮。</p>
<p>我的代码是:</p>
<p>ControllerOne.swift</p>
<pre><code>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?.badgeValue = self.count_total_notifications_ws?.description
}else{
//Here I need to show a showNotificationsbtn button
}
}
}
}
</code></pre>
<p>ControllerTwo.swift</p>
<pre><code>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
}
}
</code></pre>
<p>有人知道我该怎么做吗?
感谢您的帮助。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>在 ViewControllerOne 中</strong></p>
<pre><code>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?.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 )
}
}
</code></pre>
<p><strong>在 ViewControllerTwo 中</strong></p>
<pre><code> 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
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在另一个 Controller 上显示一个按钮,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/48378264/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/48378264/
</a>
</p>
页:
[1]