简体   繁体   English

我如何知道是否以编程方式迅速更改了标签栏控制器索引?

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

So I am writing a little something in swift that uses a tab bar controller with four tabs, each are fully functioning on their own. 因此,我在快速编写一些东西,它使用带有四个选项卡的选项卡栏控制器,每个选项卡都可以独立运行。 However in the main tab I select the best value from an array and provide a link to it. 但是,在主选项卡中,我从数组中选择最佳值并提供指向它的链接。 The array is presented in the second tab. 该阵列显示在第二个选项卡中。

I have a code like this: 我有这样的代码:

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
    }

I present the items of the array on the second tab based on array index. 我根据数组索引在第二个选项卡上显示数组的项目。 How do I pass the index (I store it in datamanager already) to the second tab OR how do I know on the second tab that it was presented using the button on the first one? 如何将索引(已经将其存储在datamanager中)传递给第二个选项卡,或者如何在第二个选项卡上知道使用第一个按钮显示的索引?

You can try these options (in order of "recommendedness"): 您可以尝试以下选项(按“推荐”顺序):

  1. keep a reference to the second controller (or retrieve it ad hoc from the tab bar controller) and implement a message in the second controller that the first controller can call explicitly 保留对第二个控制器的引用(或从选项卡栏控制器临时检索它),并在第二个控制器中实现一条消息,第一个控制器可以显式调用
  2. send an NSNotification via NSNotificationCenter and listen to it in the second controller 发送NSNotification经由NSNotificationCenter和收听它在第二控制器
  3. (not recommended) use some kind of global variable that the second controller can check (不推荐)使用第二个控制器可以检查的某种全局变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在swift中设置标签栏控制器的默认选项卡 - How do I set the default tab for tab bar controller in swift Swift:以编程方式嵌入标签栏控制器后,导航栏消失 - Swift: Navigation Bar disappears after programatically embedding tab bar controller Swift-如何将Tab Bar和导航栏添加到单个视图控制器? - Swift-How do I add Tab Bar AND Navigation Bar to a single view controller? 如何以编程方式将视图固定到Swift中的标签栏? - How to programatically fix a view to a tab bar in Swift? 如何使用swift从菜单标签栏控制器向UIViewController添加UIView? - How do I add a UIView to a UIViewController from the menu tab bar controller with swift? 如何以编程方式从选项卡栏控制器中卸载视图控制器? - How to programatically unload a view controller from Tab bar controller? 如何设置标签栏控制器的默认标签 - How do I set the default tab for tab bar controller 我无法以编程方式将标题设置为选项卡栏控制器 - I can not set title to my programatically tab bar controller 如何知道何时按下标签栏项目(iOS) - How do I know when a tab bar item is pressed (iOS) 如何以编程方式在我的标签栏控制器下添加UIView? - How can i add a UIView under my tab bar controller programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM