我如何知道选项卡栏控制器索引是否在 swift 中以编程方式更改?

How do I know if Tab Bar Controller index was changed programatically in swift?

所以我在 swift 中写了一些东西,它使用带有四个选项卡的选项卡栏控制器,每个选项卡都可以独立运行。但是在主选项卡中,我 select 数组中的最佳值并向其提供 link。该数组显示在第二个选项卡中。

我有这样的代码:

override func viewWillAppear(animated: Bool) {
        // some other stuff

        if dataManager.ideas.count > 0 {
            lblAllTimeBest.text = "Our all time best with a rating of \(dataManager.ideas[dataManager.allTimeBest].averageRating)% is:"

            btnAllTimeBest.hidden = false
             btnAllTimeBest.setTitle(dataManager.ideas[dataManager.allTimeBest].title, forState: UIControlState.Normal)

        //and some more stuff
    }

    @IBAction func btnAllTimeBestTapped(sender: AnyObject) {
            tabBarController?.selectedIndex = 1
    }

我根据数组索引在第二个选项卡上显示数组的项目。我如何将索引(我已经将其存储在数据管理器中)传递到第二个选项卡,或者我如何知道在第二个选项卡上它是使用第一个选项卡上的按钮显示的?

您可以尝试这些选项(按 "recommendedness" 的顺序):

  1. 保留对第二个控制器的引用(或从选项卡栏控制器临时检索它)并在第二个控制器中实现第一个控制器可以显式调用的消息
  2. 通过NSNotificationCenter发送NSNotification并在第二个控制器中收听
  3. (不推荐)使用某种第二个控制器可以检查的全局变量