2011-08-22 70 views
2

我一直在處理從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在運行時訪問公共屬性。有誰知道我要去哪裏錯了?

+0

「public override ViewDidLoad()」將不能編譯。請提供一些足夠有用的東西來幫助那些希望幫助你的人:-) – poupou

回答

1

有些事情必須在調用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; 
... 

編輯:在.ctor中去除的NO-OP的背景設定。增加了FinishedLaunching示例代碼。

+0

這裏有一些問題,即我想使用一個屬性和你的調用來改變ctor中的BackgroundColor基本上是一個no-op ,但使用基於ctor的方法並刪除其他ctors會導致像我的第一個示例中那樣忽略BackgroundColor,即使在保留myTabView時也是如此,但感謝您的付出。 –

+0

是的,它是一個沒有操作(你可以刪除它,這是從我以前的嘗試遺留下來的),但它確實允許你在創建控制器之後選擇背景顏色(這裏不會忽略),而不會破壞初始化。我將編輯 – poupou

+0

樣本編輯。無論如何,主要的一點是有些事情只能在特定的時間完成。添加一些Console.WriteLine行會告訴你何時被調用(例如,當'init'沒有被調用時,順序會不同,這是你的兩個樣本之間的主要區別)。 – poupou