gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:44:22 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com