0

Pass value from one TabBar controller to another Controller and set value.

I have 5 UIViewcontroller which is added to UITabBarController.

controller1 = new Controller1();
controller2 = new Controller2();
controller3 = new Controller3();
controller4 = new Controller4();
controller5 = new Controller5();

controller1.TabBarItem = new UITabBarItem ("first", UIImage.FromFile("/Images/first.png"), 0);
controller5.TabBarItem = new UITabBarItem ("second", UIImage.FromFile("/Images/four.png"), 1);
controller5.TabBarItem = new UITabBarItem ("third", UIImage.FromFile("/Images/four.png"), 2);
controller5.TabBarItem = new UITabBarItem ("four", UIImage.FromFile("/Images/four.png"), 3);
controller5.TabBarItem = new UITabBarItem ("five", UIImage.FromFile("/Images/four.png"), 4);

var tabs = new UIViewController[] {
        controller1, controller2, controller3, controller4, controller5 
    };
    ViewControllers = tabs;

controller1 have Label

UILabel lbl_Label1;  
string setValue = "Welcome";
lbl_Label1.Text = setValue;

from controller3 change controller1 Label value

create Object from Controller1 and set Label text.

Controller1 controller1 = new Controller1();
controller1.lbl_Label1.Text = "Bye";
this.TabBarController.SelectedIndex = 0;

Which is not working. Its shows same Welcome message for the label of controller1.

kiran
  • 4,285
  • 7
  • 53
  • 98

2 Answers2

0

It doesn't work because you create another instance of Controller1.

Usually, i like to use notification in this cases, try this:

Add this code in Controller3 when you want to change the text label:

NSDictionary dict = [NSDictionary dictionaryWithObject:@"new label text"
                                              forKey:@"newText"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"changeTextLabel" object:nil userInfo:dict];

And this in viewDidLoad of Controller1

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTextLabelMethod:) name:@"changeTextLabel" object:nil];

When you send notification from Controller3, Controller1 calls the method "changeTextLabelMethod" and you can do everything you want:

For Example, in Controller1:

-(void) changeTextLabelMethod:(NSNotification *) notification{
    NSDictionary *dict = [notification userInfo];
    lbl_Label1.Text = [dict objectForKey:@"newText"];
}
lubilis
  • 3,942
  • 4
  • 31
  • 54
0

While notifications could work, there is a much simpler way to do this:

You can access the viewControllers in your tab bar by calling on tabBarController --> viewControllers.

So:

this.tabBarController.viewControllers

should give you an array of viewControllers. If you are trying to modify the first view controller based on something happening in another viewController, you could do this:

Controller1 *myController1Instance = (Controller 1 *) this.tabBarController.viewControllers[0];
myController1Instance.lbl_Label1.Text = "Bye";
this.TabBarController.SelectedIndex = 0;

Assuming that lbl_Label1 is a public property on Controller1.

BHendricks
  • 4,423
  • 6
  • 32
  • 59
  • I am using Xamarin C# Lanauge. still its not working.. for me. – kiran Aug 14 '14 at 19:38
  • Ah, I missed that part, sorry I'm unfamiliar with that language. Were you able to understand what I did in Objective-C though? maybe there is an equivalent you can find in Xamarin? – BHendricks Aug 14 '14 at 20:07