gpt4 book ai didi

c# - MonoTouch 中自定义 TabBarController 中的 ViewDidLoad 中的公共(public)属性始终为 null

转载 作者:太空宇宙 更新时间:2023-11-03 14:13:24 24 4
gpt4 key购买 nike

我一直在研究从 UITabBarController 派生的类。在最基本的层面上,我想要做的是添加一个 BackgroundColor 属性和其他公共(public)属性,我可以在构建 TabBarController 时在我的 AppDelegate 中对其进行实例化。

我遇到的问题是,我可以在调用 ViewDidLoad 时让所有公共(public)属性最终为空,或者,我可以添加一个构造函数和一个 [Register] 属性,并最终使属性不为空,但是 ViewControllers 属性(包含所有选项卡)莫名其妙地变为 null(即使您在 ViewDidLoad 中设置它)。

显然,我需要两件事都是真实的,但我遗漏了一些特定的东西。

这是始终导致空 BackgroundColor 的代码版本,即使我在 AppDelegate 中明确设置它也是如此:

public class TabBarController : UITabBarController
{
public UIColor BackgroundColor { get; set; }

public override ViewDidLoad()
{
base.ViewDidLoad();

if(BackgroundColor != null) // Always null, even when set explicitly
{
var frame = new RectangleF(0.0f, 0.0f, this.View.Bounds.Size.Width, 46);
UIView myTabView = new UIView(frame);
myTabView.BackgroundColor = BackgroundColor;
myTabView.Alpha = 0.5f;
this.TabBar.InsertSubview(myTabView, 0);
}

// Add tabs here, which show up correctly (on default background color)
ViewControllers = new UIViewController[] { one, two, three, etc };
}
}

这是经过编辑的代码,它显示了正确的背景颜色(该属性不为空),但拒绝允许 ViewControllers 属性为空以外的任何值,即使它只是在 ViewDidLoad 中设置也是如此:

[Register("TabBarController")]
public class TabBarController : UITabBarController
{
public UIColor BackgroundColor { get; set; }

// Added a binding constructor
[Export("init")]
public TabBarController() : base(NSObjectFlag.Empty)
{

}

public override ViewDidLoad()
{
base.ViewDidLoad();

if(BackgroundColor != null) // Hey, this works now!
{
var frame = new RectangleF(0.0f, 0.0f, this.View.Bounds.Size.Width, 46);
UIView myTabView = new UIView(frame);
myTabView.BackgroundColor = BackgroundColor;
myTabView.Alpha = 0.5f;
this.TabBar.InsertSubview(myTabView, 0);
}

// Tabs disappear, ViewControllers is always null
ViewControllers = new UIViewController[] { one, two, three, etc };
if(ViewControllers == null)
{
Console.WriteLine("Not bro");
}
}
}

如果我必须显式添加所有元素而不能在运行时访问公共(public)属性,这显然会阻碍我编写一些自定义控件的能力。有谁知道我哪里出错了?

最佳答案

在调用 ViewDidLoad 之前必须发生一些事情。它们可以在构造函数中完成。但是下面的构造函数是坏的:

 public TabBarController() : base(NSObjectFlag.Empty)

因为它不允许 UITabController 默认 ctor 执行 - 它的工作是向“init”选择器发送消息。

我觉得你想要的有点像:

public class TabBarController : UITabBarController
{
UIViewController one = new UIViewController ();
UIViewController two = new UIViewController ();
UIViewController three = new UIViewController ();

private UIView myTabView;

public UIColor BackgroundColor {
get { return myTabView.BackgroundColor; }
set { myTabView.BackgroundColor = value; }
}

public TabBarController()
{
var frame = new RectangleF(0.0f, 0.0f, this.View.Bounds.Size.Width, 46);
myTabView = new UIView(frame);
myTabView.Alpha = 0.5f;
this.TabBar.InsertSubview(myTabView, 0);

// Add tabs here, which show up correctly (on default background color)
ViewControllers = new UIViewController[] { one, two, three };
}
}

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
TabBarController controller = new TabBarController ();
// change background (to cyan) works before adding subview
controller.BackgroundColor = UIColor.Cyan;
window.AddSubview (controller.View);
// change background (to blue) works after adding subview
controller.BackgroundColor = UIColor.Blue;
...

编辑:删除了 .ctor 中的无操作背景设置。添加了 FinishedLaunching 示例代码。

关于c# - MonoTouch 中自定义 TabBarController 中的 ViewDidLoad 中的公共(public)属性始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7142441/

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