【问题标题】:How can you update a label when the tab/view controller of a tab bar changes? (swift)当标签栏的标签/视图控制器更改时,如何更新标签? (迅速)
【发布时间】:2021-08-09 02:39:14
【问题描述】:

我正在开发一个带有带有四个单独标签的标签栏的应用程序。例如,我保存了一个可以在其中三个选项卡中更改/修改的数字。显示该数字的标签包含在所有四个选项卡中。但是,当我更改一个选项卡中的数字时,当我切换选项卡时,其他选项卡的标签不会更新。

我尝试在标签的视图控制器的每个 viewDidLoad() 中包含这个:

self.tabBarController?.delegate = self 

然后使用:

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        let tabBarIndex = tabBarController.selectedIndex
        if tabBarIndex == 2{
            updateLabel()
        }
}

如果我在所有 viewControllers 中执行此操作并选择选项卡,视图仍​​会更改,但 tabBarController 失败,因此根本不会调用函数 updateLabel()。 如果我只在First View Controller中包含代码并展开这部分:

if tabBarIndex == 2{
            updateLabel()
        }

为了覆盖所有选项卡,调用了类的相应函数(updateLabel()),但 Label 本身为 nil。

@IBOutlet weak var HoursLabel: UILabel!

func updateLabel(){
        if HoursLabel != nil{
             //code
        }    
}

并且标签没有更新。 有人知道如何解决这个问题吗?提前非常感谢你:)

【问题讨论】:

    标签: ios swift xcode uiviewcontroller uitabbarcontroller


    【解决方案1】:

    嘿,我为你找到了一些解决方案,希望这是你想要的。

    我正在使用通知中心更新您的标签标签值,这是一些代码

    // TabbarController 代码

    import UIKit
    
    class TabbarViewController: UITabBarController, UITabBarControllerDelegate {
    
    //MARK Life View Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
    
        tabBarController?.delegate = self
        NotificationCenter.default.addObserver(self, selector: #selector(self.TabbarNoitifuntionCall), name: NSNotification.Name(rawValue: "CallTabBarNotificationsCenter"), object: nil) // this code will call when ever you update your value in view controller
    }
    
    //MARK:- Private Functions
    @objc func TabbarNoitifuntionCall(_ notification: Notification) {
        self.viewControllers![0].title = "First " + String(notification.object as! Int)
        self.viewControllers![1].title = "Second " + String(notification.object as! Int)
       }
    
    }
    

    // 第一个标签栏控制器

     import UIKit
    
     class FirstVc: UIViewController {
    
    //MARK:- IBOutlet
    @IBOutlet weak var update_lbl: UILabel!
    
    //MARK:- Variables
    var count = Int()
    
    //MARK:- Life View Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    //MARK:- Private Function
    @IBAction func buttonAction(_ sender: UIButton) {
        
        count = count + 1
        update_lbl.text = String(count)
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "CallTabBarNotificationsCenter"), object: count) // here we will call notifications center so our labal value upadate
        
       }
    
    }
    

    // 第二个标签栏控制器

      import UIKit
    
       class SecondVc: UIViewController {
    
    //MARK:- IBOutlet
    @IBOutlet weak var update_lbll: UILabel!
    
    //MARK:- Variables
    var count = Int()
    
    //MARK:- Life View Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    //MARK:- Private Function
    @IBAction func button_action(_ sender: Any) {
        count = count + 1
        update_lbll.text = String(count)
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "CallTabBarNotificationsCenter"), object: count) // here we will call notifications center so our labal value upadate
       }
    
    }
    

    我希望这是一种简单的方法,如果有人添加一些东西,那么请继续 谢谢。

    【讨论】:

      猜你喜欢
      • 2016-12-17
      • 2015-04-23
      • 2020-07-29
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 2017-08-23
      • 1970-01-01
      相关资源
      最近更新 更多