所以我用 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
}
我根据数组索引在第二个选项卡上显示数组的项目。我如何将索引(我已经将其存储在数据管理器中)传递到第二个选项卡,或者我如何知道在第二个选项卡上它是使用第一个选项卡上的按钮显示的?
最佳答案
您可以尝试这些选项(按“推荐”顺序):
- 保留对第二个 Controller 的引用(或从选项卡栏 Controller 临时检索它)并在第二个 Controller 中实现第一个 Controller 可以显式调用的消息
- 通过
NSNotificationCenter
发送一个NSNotification
并在第二个 Controller 中收听它 - (不推荐)使用某种全局变量,第二个 Controller 可以检查
关于ios - 我怎么知道 Tab Bar Controller 索引是否以编程方式快速更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31217900/