我一直在处理从UITabBarController派生的类。在最基本的层面上,我想要做的是添加一个BackgroundColor属性和其他公共属性,当我构建TabBarController时,可以在我的AppDelegate中实例化这些属性。公共属性在ViewDidLoad中的MonoTouch中的自定义TabBarController中始终为空
我遇到的问题是,我可以让所有的公共属性在调用ViewDidLoad时最终为null,或者,我可以添加一个构造函数和一个[Register]属性,并最终得到属性不为null ,但ViewControllers属性(包含所有选项卡)变得莫名其妙(即使您在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 };
}
}
下面是编辑的代码显示了正确的背景颜色(属性是NOT NULL),但拒绝让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");
}
}
}
这显然阻碍了我写一些自定义的控件,如果我要明确地添加所有元素没有能力能够o在运行时访问公共属性。有谁知道我要去哪里错了?
“public override ViewDidLoad()”将不能编译。请提供一些足够有用的东西来帮助那些希望帮助你的人:-) – poupou