2011-08-22 71 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