我一直在處理從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