Question

I have one page(say X) before tab bar controller that use model segue to open tab bar controller. Also first screen of tab bar controller is same with X.

I want to pass data from X to tab bar controller's first page.

Shortly, I want to pass data from view controller to tab bar controller page with storyboard segue. Is there any method for this ?

Here is the solution ;

    locationsHome* vc = [[locationsHome alloc] init];
    UITabBarController* tbc = [segue destinationViewController];
    vc = (locationsHome *)[[tbc customizableViewControllers] objectAtIndex:0];
Was it helpful?

Solution

This is how I solved my problem. You can pass data from ViewController to TabBarController with this method.

Use this code within prepareForSegue method

locationsHome* vc = [[locationsHome alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (locationsHome *)[[tbc customizableViewControllers] objectAtIndex:0];

Like this:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    NSString * segueIdentifier = [segue identifier];
    if([segueIdentifier isEqualToString:@"tabbarGo"]){

        locationsHome* vc = [[locationsHome alloc] init];
        UITabBarController* tbc = [segue destinationViewController];
        vc = (locationsHome *)[[tbc customizableViewControllers] objectAtIndex:0];

        etc...

    }
}

OTHER TIPS

//if sent to First Tab of Tab Bar Controller
UITabBarController *tabBarController = segue.destinationViewController;
UINavigationController *navigationController = (UINavigationController *)[[tabBarController viewControllers] objectAtIndex:0];
createTeamViewController *controller = (createTeamViewController *)[[navigationController viewControllers] objectAtIndex:0];

controller.userProfile = self.userProfile;

Add a property to the destination view controller (the subclassed tab bar controller) and then in the source view controller, implement prepareForSegue to pass information to the destination controller. You can then have the destination view controller's viewDidLoad to act upon the data that was passed to it in the prepareForSegue of the source view controller.

For Swift 2.2, I had to modify accepted answer this way

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if (segue.identifier == "userAccountSegue")
    {
        var firstTabViewController: UserAccountViewController = UserAccountViewController()
        let tabViewController: UITabBarController = segue.destinationViewController as! UITabBarController
        firstTabViewController = (tabViewController.customizableViewControllers![0] as! UserAccountViewController)
        firstTabViewController.userObj = arrayForTable![selectedIndex] as? User

    }
}

Note : Hint for above code snip

  • User is my Model object storing user information
  • My View controller segue is connected to the TabViewContoller in storyboard with segue = "userAccountSegue"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top