c# - 如何从在 Xamarin IOS 中添加 UITabBarController 的另一个 Controller 更改标签的文本值

标签 c# ios iphone xamarin uitabbarcontroller

我在 tabbarcontroller 中添加了两个 View Controller 。
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;
        }
    }
}

//信息 Controller 类

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

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

从 UITabBarController 添加了另一个 viewController,它是 selectedController, 有值(value) name = "Iphone Ipad";

在selectedController中的某些操作上,lbl_Name.Text的infoController变为"Iphone Ipad"

//SelectedController 类。

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

它转移到 0 选定的 TabBarController 索引。但是 lbl_Name 的值是一样的,

如何从另一个 Controller 更改标签的文本值?

最佳答案

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

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 上的按钮时,它会更新其父 Controller 中的公共(public)属性。现在,当 ControllerB 即将显示时,您可以使用 WillAppear 或 DidAppear 从模型更新标签的文本(在本例中为父 Controller 中的 StringB 属性)

关于c# - 如何从在 Xamarin IOS 中添加 UITabBarController 的另一个 Controller 更改标签的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25331454/

相关文章:

c# - 如何告诉 Fluent NHibernate 不要映射类属性

c# - 如何在 C# 中将 SQL 解析为数据表或数组?

ios - Web 服务如何工作?

ios - 文本右侧带有图像的 UIButton,按钮边缘的尾部空格固定

iphone - 在 iPhone SDK 4.0 中使用 MPMovieController 的问题

iphone - iOS: [X convertPoint: somePoint toView: nil];无法返回窗口坐标

iphone - 使用iOS gamekit框架可以连接两个设备的范围(距离)是多少

c# - 网络协议(protocol)的实现

c# - 如何将EPPlus中创建的图表导出到图像文件

objective-c - 将数组分配给变量,然后切换一系列条件语句