在swift中将数据从一个选项卡控制器传递到另一个

时间:2021-12-07 13:53:56

My application has three viewController associated with three tab of tabBar. I want to switch from 1st tab to 3rd tab and pass some data from 1st view controller to 3rd view controller. I can't do it with segue, because segue create navigation within the selected tab. That is not my requirement. I want to switch tab in tabBar and pass some data without any navigation. How can i do it ?

我的应用程序有三个与tabBar三个tab相关联的viewController。我想从第一个选项卡切换到第三个选项卡,并将一些数据从第一个视图控制器传递到第三个视图控制器。我不能用segue做,因为segue在所选标签内创建导航。那不是我的要求。我想在tabBar中切换选项卡并传递一些数据而不进行任何导航。我该怎么做 ?

3 个解决方案

#1


4  

you can use this code : Objective C

你可以使用这段代码:Objective C

[tab setSelectedIndex:2];

save your array in NSUserDefaults like this:

将您的数组保存在NSUserDefaults中,如下所示:

[[NSUserDefaults standardUserDefaults]setObject:yourArray forKey:@"YourKey"];

and get data from another view using NSUserDefaults like this :

并使用NSUserDefaults从另一个视图获取数据,如下所示:

NSMutableArray *array=[[NSUserDefaults standardUserDefaults]objectForKey:@"YourKey"];

swift

迅速

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if segue.identifier == "toTabController" {
        var tabBarC : UITabBarController = segue.destinationViewController as UITabBarController
        var desView: CaseViewController = tabBarC.viewControllers?.first as CaseViewController

        var caseIndex = overviewTableView!.indexPathForSelectedRow()!.row
        var selectedCase = self.cases[caseIndex]

        desView.caseitem = selectedCase
      }
    }

#2


10  

Swift 3 in latest Xcode version 8.3.3

最新的Xcode版本8.3.3中的Swift 3

First you can set a tab bar delegate UITabBarControllerDelegate in firstViewController. You can use this when you want to send data from one view controller to another by clicking the tab bar button in UITabBarViewController.

首先,您可以在firstViewController中设置标签栏委托UITabBarControllerDelegate。当您想通过单击UITabBarViewController中的标签栏按钮将数据从一个视图控制器发送到另一个视图控制器时,可以使用此选项。

class firstViewController: UIViewController ,UITabBarControllerDelegate {
    let arrayName = ["First", "Second", "Third"] 

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBarController?.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController.isKind(of: firstsubsecoundViewController.self as AnyClass) {
            let viewController  = tabBarController.viewControllers?[1] as! secondViewController
            viewController.arrayData = self.arrayName
        } 

        return true
    }
}

Get data in secondViewController:

在secondViewController中获取数据:

class secondViewController: UIViewController {
    var arrayData: [String] = NSArray()
    override func viewDidLoad() {
       super.viewDidLoad()
       print(arrayData) // Output: ["First", "Second", "Third"]
   }
}

#3


2  

Okay, i tried with creating a singleton object in viewController of first tab and then get that object's value from viewController of third tabBar. It works only for once when third tab's view controller instantiates for the 1st time. I never got that singleton object's value in third tab's view controller except the first time. What can i do now ? In my code - In first tab's controller, if i click a button tab will be switched to third tab. Below is my code portion -

好吧,我尝试在第一个选项卡的viewController中创建一个单例对象,然后从第三个tabBar的viewController获取该对象的值。当第三个选项卡的视图控制器第一次实例化时,它只能工作一次。除了第一次,我从未在第三个选项卡的视图控制器中获得该单例对象的值。我现在能做什么 ?在我的代码中 - 在第一个选项卡的控制器中,如果我单击一个按钮选项卡将切换到第三个选项卡。以下是我的代码部分 -

In First tab's controller -

在First标签的控制器中 -

@IBAction func sendBtnListener(sender: AnyObject) {
        Singleton.sharedInstance.brandName = self.searchDisplayController!.searchBar.text
        self.tabBarController!.selectedIndex = 2
}

In Third tab's Controller -

在第三个标签的控制器中 -

 override func viewDidLoad() {
     super.viewDidLoad()

     //Nothing is printed out for this portion of code except the first time 
     if !Singleton.sharedInstance.brandName.isEmpty{
         println(Singleton.sharedInstance.brandName)

     }else{

         println("Empty")
     }
 }

In Singleton Object's class -

在Singleton Object的类中 -

class Singleton {
    var name : String = ""
    class var sharedInstance : Singleton {
        struct Static {
            static let instance : Singleton = Singleton()
        }
        return Static.instance
    }

    var brandName : String {
        get{
            return self.name
        }

        set {
            self.name = newValue
        }
    }
} 

Edited :

编辑:

Okay at last it's working. For others who just want like me, please replace all the code from viewDidLoad() to viewWillAppear() method in third tab's (destination tab) controller and it will work. Thanks.

好吧,最后它正在运作。对于那些只想和我一样的人,请将viewDidLoad()中的所有代码替换为第三个选项卡(目标选项卡)控制器中的viewWillAppear()方法,它将起作用。谢谢。

#1


4  

you can use this code : Objective C

你可以使用这段代码:Objective C

[tab setSelectedIndex:2];

save your array in NSUserDefaults like this:

将您的数组保存在NSUserDefaults中,如下所示:

[[NSUserDefaults standardUserDefaults]setObject:yourArray forKey:@"YourKey"];

and get data from another view using NSUserDefaults like this :

并使用NSUserDefaults从另一个视图获取数据,如下所示:

NSMutableArray *array=[[NSUserDefaults standardUserDefaults]objectForKey:@"YourKey"];

swift

迅速

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if segue.identifier == "toTabController" {
        var tabBarC : UITabBarController = segue.destinationViewController as UITabBarController
        var desView: CaseViewController = tabBarC.viewControllers?.first as CaseViewController

        var caseIndex = overviewTableView!.indexPathForSelectedRow()!.row
        var selectedCase = self.cases[caseIndex]

        desView.caseitem = selectedCase
      }
    }

#2


10  

Swift 3 in latest Xcode version 8.3.3

最新的Xcode版本8.3.3中的Swift 3

First you can set a tab bar delegate UITabBarControllerDelegate in firstViewController. You can use this when you want to send data from one view controller to another by clicking the tab bar button in UITabBarViewController.

首先,您可以在firstViewController中设置标签栏委托UITabBarControllerDelegate。当您想通过单击UITabBarViewController中的标签栏按钮将数据从一个视图控制器发送到另一个视图控制器时,可以使用此选项。

class firstViewController: UIViewController ,UITabBarControllerDelegate {
    let arrayName = ["First", "Second", "Third"] 

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBarController?.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController.isKind(of: firstsubsecoundViewController.self as AnyClass) {
            let viewController  = tabBarController.viewControllers?[1] as! secondViewController
            viewController.arrayData = self.arrayName
        } 

        return true
    }
}

Get data in secondViewController:

在secondViewController中获取数据:

class secondViewController: UIViewController {
    var arrayData: [String] = NSArray()
    override func viewDidLoad() {
       super.viewDidLoad()
       print(arrayData) // Output: ["First", "Second", "Third"]
   }
}

#3


2  

Okay, i tried with creating a singleton object in viewController of first tab and then get that object's value from viewController of third tabBar. It works only for once when third tab's view controller instantiates for the 1st time. I never got that singleton object's value in third tab's view controller except the first time. What can i do now ? In my code - In first tab's controller, if i click a button tab will be switched to third tab. Below is my code portion -

好吧,我尝试在第一个选项卡的viewController中创建一个单例对象,然后从第三个tabBar的viewController获取该对象的值。当第三个选项卡的视图控制器第一次实例化时,它只能工作一次。除了第一次,我从未在第三个选项卡的视图控制器中获得该单例对象的值。我现在能做什么 ?在我的代码中 - 在第一个选项卡的控制器中,如果我单击一个按钮选项卡将切换到第三个选项卡。以下是我的代码部分 -

In First tab's controller -

在First标签的控制器中 -

@IBAction func sendBtnListener(sender: AnyObject) {
        Singleton.sharedInstance.brandName = self.searchDisplayController!.searchBar.text
        self.tabBarController!.selectedIndex = 2
}

In Third tab's Controller -

在第三个标签的控制器中 -

 override func viewDidLoad() {
     super.viewDidLoad()

     //Nothing is printed out for this portion of code except the first time 
     if !Singleton.sharedInstance.brandName.isEmpty{
         println(Singleton.sharedInstance.brandName)

     }else{

         println("Empty")
     }
 }

In Singleton Object's class -

在Singleton Object的类中 -

class Singleton {
    var name : String = ""
    class var sharedInstance : Singleton {
        struct Static {
            static let instance : Singleton = Singleton()
        }
        return Static.instance
    }

    var brandName : String {
        get{
            return self.name
        }

        set {
            self.name = newValue
        }
    }
} 

Edited :

编辑:

Okay at last it's working. For others who just want like me, please replace all the code from viewDidLoad() to viewWillAppear() method in third tab's (destination tab) controller and it will work. Thanks.

好吧,最后它正在运作。对于那些只想和我一样的人,请将viewDidLoad()中的所有代码替换为第三个选项卡(目标选项卡)控制器中的viewWillAppear()方法,它将起作用。谢谢。