切换标签栏包括发送数据

时间:2016-11-21 07:21:48

标签: ios iphone swift xcode uitabbarcontroller

如何更改标签栏?我知道这篇文章似乎重复,但我找不到任何类似于我的问题。现在我的当前

  

selectedIndex = 0

所以我想让它转到第3号标签

  

selectedIndex = 2

但我也想将数据从currentView发送到nextView。如果我使用push + selectedindex,它将转到选项卡3但是从selectedindex = 0推送视图,并且没有数据发送到selectedIndex = 2 我目前的代码

func redeemBtnPressed(_ sender: UIButton) {

        let selectedRedeemBtnInfo = fixedGridInfo[sender.tag] as! Dictionary<String, AnyObject>
        sender.showsTouchWhenHighlighted = true

        let storyboard = UIStoryboard(name: "FlightExploration", bundle: nil)
        let searchFlightVC = storyboard.instantiateViewController(withIdentifier: "SearchFlightVC") as! SearchFlightViewController

        var newFlightType = String()

        if "\(selectedRedeemBtnInfo["FlightType"]!)" == "Return" {
            newFlightType = "Round"
        } else {
            newFlightType = "One"
        }

        searchFlightVC.flightType = newFlightType
        searchFlightVC.fromHome = true
        searchFlightVC.departure = "\(selectedRedeemBtnInfo["Departure"]!) (\(selectedRedeemBtnInfo["DepartureCityCode"]!)"
        searchFlightVC.arrival = "\(selectedRedeemBtnInfo["Destination"]!) (\(selectedRedeemBtnInfo["DestinationCityCode"]!)"
        self.navigationController?.pushViewController(searchFlightVC, animated: true)
        tabBarController?.selectedIndex = 2
    }

3 个答案:

答案 0 :(得分:1)

您可以尝试将数据发送到UIViewController,它是UITabBarController的ViewController

    var yourViewController : TempViewController
    if let arrController = tabBarController?.viewControllers {
        for vc in arrController {
            if vc is TempViewController {
                yourViewController = vc as! TempViewController
            }
        }
    }

    yourViewController.yourData = dataToPass
    tabBarController?.selectedIndex = 2

答案 1 :(得分:0)

从Rajat的回答中修改了答案似乎帮助我解决了这个问题

func redeemBtnPressed(_ sender: UIButton) {

        let selectedRedeemBtnInfo = fixedGridInfo[sender.tag] as! Dictionary<String, AnyObject>
        sender.showsTouchWhenHighlighted = true

        var newFlightType = String()

        if "\(selectedRedeemBtnInfo["FlightType"]!)" == "Return" {
            newFlightType = "Round"
        } else {
            newFlightType = "One"
        }

        if let arrController = tabBarController?.viewControllers {
            for vc in arrController {
                if vc.childViewControllers[0] is SearchFlightViewController {
                    let displayViewController = vc.childViewControllers[0] as! SearchFlightViewController
                    let _ = displayViewController.navigationController?.popToRootViewController(animated: true)
                    //displayViewController.flightType = newFlightType
                    displayViewController.flightTypeFromHome = newFlightType
                    displayViewController.fromHome = true
                    displayViewController.departure = "\(selectedRedeemBtnInfo["Departure"]!) (\(selectedRedeemBtnInfo["DepartureCityCode"]!)"
                    displayViewController.arrival = "\(selectedRedeemBtnInfo["Destination"]!) (\(selectedRedeemBtnInfo["DestinationCityCode"]!)"
                    displayViewController.flightType = "Round"

                    displayViewController.departureDateLbl = "Select One"
                    displayViewController.passenger = "1 Adult"
                    displayViewController.adultCount = 1
                    displayViewController.childCount = Int()
                    displayViewController.infantCount = Int()
                    tabBarController?.selectedIndex = 2
                    tabBarController?.tabBar((tabBarController?.tabBar)!, didSelect: (tabBarController?.tabBar.items?[2])!)
                }
            }
        }

    }

答案 2 :(得分:0)

Rajat的答案Switch Tab Bar和Pass数据,在 Swift 4.2,iOS 11 中需要进行一些更改:

func switchToTab2(){}
    var yourViewController = MyTab2ViewController()
    if let arrController = self.tabBarController?.viewControllers {
        for vc in arrController {
            if vc is MyTab2ViewController {
                yourViewController = vc as! MyTab2ViewController
                yourViewController.productTitle = "Title"
                self.tabBarController?.selectedIndex = 1 /// tabs start from 0
            }
        }
    }  
}

MyTab2ViewController 是连接到具有索引1的选项卡栏的视图控制器。(第一个选项卡索引:0)