如何从另一个在Xamarin IOS中添加UITabBarController的控制器更改标签的文本值

时间:2014-08-15 17:47:24

标签: c# ios iphone xamarin uitabbarcontroller

我有tabbarcontroller添加了两个视图控制器 InformationController,SelectedController。

// UITabBarController类

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类

public string name = "";
    DisplayItems(){
     lbl_Name.Text = name;
    }

public override void ViewWillAppear (bool animated){
 base.ViewWillAppear (animated);
 this.DisplayItems();
}

从UITabBarController添加另一个viewController,它是selectedController, 有价值     name =“Iphone Ipad”;

对于selectedController中的某些操作,lbl_Name.Text的infoController更改为"Iphone Ipad";

// SelectedController Class。

InformationController infoController = new InformationController();
                    infoController.lbl_MedicineName.Text = "Iphone Ipad";
                    this.controller.TabBarController.SelectedIndex = 0;

它转移到0选择的TabBarController索引。但是lbl_Name的值是相同的,

如何更改另一个控制器的标签文本值?

1 个答案:

答案 0 :(得分:1)

我举了一个例子,你可以扩展你的代码:

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;
    }
}

在ControllerB中,LabelB在ViewDidLoad()中使用默认值进行初始化。按下TabA上的按钮时,它会更新其父控制器中的公共属性。现在,当要显示ControllerB时,可以使用WillAppear或DidAppear从模型中更新Label的文本(在本例中是父控制器中的StringB属性)