0
votes

I have a TabBarController which is set up with multiple ViewControllers at launch. When the user clicks a button I want to send them to a different ViewController in the TabBarController, and pass data through a delegate.

I have a protocol and delegate set up. However, when do you set the delegate since all the ViewControllers are in the TabBarController

Is this possible, how can I pass data to another ViewController in the TabBar when the user clicks a button. Any ideas, I'd really like to use a delegate.

- (IBAction)sendData:(id)sender
{
    [self.delegate setStringData:strData];
    self.tabBarController.selectedIndex = 0;
}

Edit:

So let's say I have a TabBarController with two ViewControllers called ViewControllerOne and ViewControllerTwo.

I have ViewControllerTwo set up as the delegate and protocol. This is the ViewController that will send data to ViewControllerOne after the button is pressed. ViewControllerOne implements protocol and contains the method setStringData which should be called after the button in ViewControllerTwo is pressed.

2
please, use some subjects... which class should be the delegate? and in which class should it be used? you are going to go from a viewController to another... then? - meronix
I'll edit to make it a little bit clearer. - Vikings

2 Answers

0
votes

From a UIViewController you want to change the selected tab bar index and pass data. I suggest you add a function in you app delegate for this. That way your UIViewController won't be tied with a UITabBar (if tomorrow you want to use a different UI idiom, you will just have to change the implementation of your function in your app delegate).

To pass data, i you could try to introspection in your function : you take the current UIViewController of the new selected tab index, verify it responds to your selector and call the function.

Edit :

Let's assume your 'just' have to change the selected tabBar index (e.g. your UIViewController will always be the same on the new tab bar index).

In your first View Controller :

    - (IBAction)sendData:(id)sender
    {
        UIApplicationDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
        if ([appDelegate respondToSelector:@selector(goToFirstTabBarWithData:)])
        {
            [appDelegate performSelector:@selector(goToFirstTabBarWithData:) withObject: strData];
        }
    }

In your Appdelegate :

    - (void)goToFirstTabBarWithData:(NSString *)data
    {
        self.tabBarController.selectedIndex = 0;
        UIViewController * vc = [self.tabBarController.viewControllers objectAtIndex:0];
        if ([vc respondToSelector:@selector(setStringData:)])
        {
            [vc performSelector:@selector(setStringData:) withObject:data];
        }
    }

In your second View controller (the one you will arrive on) :

    - (void)setStringData:(NSString *)data
    {
        // Do something...
    }
0
votes

I found a simpler solution to my problem. Inside of ViewControllerTwo, I just create an instance of ViewControllerOne and pass it that data I need. Then I change the tabBarController index to ViewControllerOne.

For example:

// A method inside of ViewControllerTwo
ViewController *viewcontrollerOne = [ViewcontrollerOne alloc] init];
[viewcontrollerOne setStringData:str];
[viewcontrollerOne release];
self.tabBarController.selectedIndex = 0;