在MonoTouch的自定义TabBarController的ViewDidLoad中,公共属性总是空的
本文关键字:属性 自定义 MonoTouch TabBarController ViewDidLoad | 更新日期: 2023-09-27 18:06:12
我一直在一个类派生自UITabBarController。在最基本的层面上,我要做的是添加一个BackgroundColor属性,以及其他公共属性,当我在构建TabBarController时,我可以在AppDelegate中实例化它们。
我遇到的问题是,我可以有所有的公共属性结束为空当ViewDidLoad被调用,或者,我可以添加一个构造函数和一个[注册]属性,并最终有属性不为空,但ViewControllers属性(其中包含所有标签)变得莫名其妙的空(即使当你在ViewDidLoad设置它)。
显然,我需要这两件事都为真,而我遗漏了一些具体的东西。
这是总是导致null 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 };
}
}
这是编辑的代码,显示正确的背景颜色(属性不是null),但拒绝允许ViewControllers属性为null以外的任何东西,即使它只是设置在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");
}
}
}
如果我必须显式地添加所有元素而不能在运行时访问公共属性,这显然会阻碍我编写一些自定义控件的能力。有人知道我哪里出错了吗?
在调用ViewDidLoad之前必须发生一些事情。它们可以在构造函数中完成。但是下面的元素是坏的:
public TabBarController() : base(NSObjectFlag.Empty)
因为它不允许UITabController的默认函数执行-它的工作是发送一个消息给'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;
...
EDIT:删除。ctor中的无op背景设置。添加FinishedLaunching示例代码