ios - 我怎么知道 Tab Bar Controller 索引是否以编程方式快速更改?

标签 ios arrays swift uitabbarcontroller

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

我有这样的代码:

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
    }

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

最佳答案

您可以尝试这些选项(按“推荐”顺序):

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

关于ios - 我怎么知道 Tab Bar Controller 索引是否以编程方式快速更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31217900/

相关文章:

ios - 在通知应用程序中找不到应用程序的有效 'aps-environment' 授权字符串

ios - 使用 parse.com 使用外部 json

ios - swift spritekit中解析Nodes位置时出现 fatal error

ios - 如何在 Swift playground 中停止帧的大小变为 1024x768

ios - 无法将框架/Pod 导入 xcode

ios - 如何设置可轻松跨越任何 iOS 设备的 UI?

ios - 从 UICollectionViewCell 传播自定义事件

java - 如何输出数组中元素的索引

c - 为什么变量求和返回乘法?

C 为什么当按值传递这个 int 时,每次都错误地传递 0