Reputation: 4409
I have tabbarcontroller added two view controller.
InformationController, SelectedController.
//UITabBarController Class
using System;
using System.Collections.Generic;
using MonoTouch.UIKit;
using System.Drawing;
namespace IOS {
public class DetailTabController : UITabBarController {
public InformationController infoController;
public SelectedController selectedController;
public string name = "MacBookPro";
public override void DidReceiveMemoryWarning (){
base.DidReceiveMemoryWarning ();
}
public override void ViewDidLoad (){
base.ViewDidLoad ();
}
public override void ViewWillAppear (bool animated){
base.ViewWillAppear (animated);
infoController = new InformationTabViewController();
infoController.name = this._name;
infoController.TabBarItem = new UITabBarItem ("Info", UIImage.FromFile("/Images/first.png"), 0);
selectedController = new SelectedController ();
selectedController.Title = "selected";
selectedController.TabBarItem = new UITabBarItem ("Select", UIImage.FromFile("/Images/four.png"), 1);
var tabs = new UIViewController[] {
infoController, selectedController
};
ViewControllers = tabs;
}
}
}
// InformationController Class
public string name = "";
DisplayItems(){
lbl_Name.Text = name;
}
public override void ViewWillAppear (bool animated){
base.ViewWillAppear (animated);
this.DisplayItems();
}
From UITabBarController added another viewController which is selectedController, have value name = "Iphone Ipad";
on some operation in selectedController, the infoController of lbl_Name.Text
changes to "Iphone Ipad"
;
// SelectedController Class.
InformationController infoController = new InformationController();
infoController.lbl_MedicineName.Text = "Iphone Ipad";
this.controller.TabBarController.SelectedIndex = 0;
Its shift to 0 selected Index of TabBarController. But The value of lbl_Name is same,
How to change the text value of label from another controller?
Upvotes: 0
Views: 2534
Reputation: 424
I made a little example that you can expand upon for your code:
MyTabBarController.cs:
public class MyTabBarController : UITabBarController
{
public string StringB
{
get;
set;
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
ViewControllers = new UIViewController[]
{
new ControllerA
{
TabBarItem = new UITabBarItem("A", null, 0),
},
new ControllerB
{
TabBarItem = new UITabBarItem("B", null, 1),
},
};
}
}
ControllerA.cs:
public class ControllerA : UIViewController
{
private int counter = 0;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
View.BackgroundColor = UIColor.White;
var button = UIButton.FromType(UIButtonType.RoundedRect);
button.Frame = View.Frame;
button.SetTitle("Click me to change B's Text", UIControlState.Normal);
button.TouchUpInside += (sender, e) =>
{
var parentController = ParentViewController as MyTabBarController;
if (parentController != null)
{
parentController.StringB = "Here's a new string for you";
}
};
View.AddSubview(button);
}
}
ControllerB.cs:
public class ControllerB : UIViewController
{
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
View.BackgroundColor = UIColor.White;
LabelB = new UILabel(View.Frame)
{
Text = "B's Default Text",
TextColor = UIColor.Black,
};
View.AddSubview(LabelB);
}
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
var parent = ParentViewController as MyTabBarController;
if (parent != null && !string.IsNullOrEmpty(parent.StringB))
{
LabelB.Text = parent.StringB;
}
}
public UILabel LabelB {
get;
set;
}
}
In ControllerB, LabelB is initialized with a default value in ViewDidLoad(). When the button on TabA is pressed, it updates a public property in their parent controller. Now when ControllerB is about to be displayed you can use WillAppear or DidAppear to update your Label's text from your Model (in this case the StringB property in your parent controller)
Upvotes: 1