在MonoTouch中的自定义TabBarController中,ViewDidLoad中的公共属性始终为null

时间:2011-08-22 01:49:40

标签: c# controls xamarin.ios uitabbarcontroller viewdidload

我一直在研究从UITabBarController派生的类。在最基本的层面上,我正在尝试添加一个BackgroundColor属性和其他公共属性,我可以在构建我的TabBarController时在AppDelegate中实例化。

我遇到的问题是,我可以让所有公共属性在调用ViewDidLoad时最终为null,或者,我可以添加构造函数和[Register]属性,并最终使属性不为null,但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");
        }
    }
}

如果我必须显式添加所有元素而不能在运行时访问公共属性,这显然会阻碍我编写一些自定义控件的能力。有谁知道我哪里出错了?

1 个答案:

答案 0 :(得分:1)

在调用ViewDidLoad之前必须发生一些事情。它们可以在构造函数中完成。然而,以下ctor是坏的:

 public TabBarController() : base(NSObjectFlag.Empty)

因为它不允许UITabController默认ctor执行 - 它的工作是向'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中的无操作背景设置。添加了FinishedLaunching示例代码。