2011-08-22 8 views
2

UITabBarController에서 파생 된 클래스에서 작업했습니다. 가장 기본적인 수준에서, 내가하려는 것은 BackgroundColor 속성과 다른 공용 속성을 추가하는 것입니다.이 속성은 TabBarController를 구성 할 때 AppDelegate에서 인스턴스화 할 수 있습니다.MonoTouch의 사용자 정의 TabBarController에서 ViewDidLoad의 공용 속성은 항상 null입니다.

내가 만난 문제는 ViewDidLoad가 호출 될 때 모든 public 속성이 null이되거나, 생성자와 [Register] 특성을 추가 할 수 있고 null이 아닌 속성을 갖게된다는 것입니다. , ViewControllers 속성 (모든 탭이 들어 있음)은 설명 할 수 없게 null이됩니다 (ViewDidLoad에서 설정 한 경우에도 마찬가지입니다).

분명히, 나는 두 가지 사실이 모두 필요하며 나는 뭔가 구체적인 것을 놓치고 있습니다. 여기

내가 AppDelegate에 명시 적으로 설정 한 경우에도 항상 널의 backgroundColor 결과 코드의 버전입니다 :

여기
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)하지만, 그것이 단지의 viewDidLoad에 설정되어있는 경우에도 ViewControllers 속성이 null하지만 아무것도 할 수 있도록 거부 :

[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 기본 ctor에 실행하는 것을 허용하지 않기 때문에 - 그 작업은 '초기화하기'선택에 메시지를 보내는 것입니다 : 그러나 다음의 ctor는 나쁘다.

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 샘플 코드를 추가했습니다.

+0

여기에 몇 가지 문제가 있습니다. 즉, 속성을 사용하고 ctor에서 BackgroundColor를 변경하기위한 호출을 기본적으로 사용하지 않으려면 아무 문제가 없습니다. 하지만 ctor 기반 접근 방식을 사용하고 다른 ctors를 제거하면 myTabView를 유지할 때조차도 첫 번째 예제에서와 같이 BackgroundColor를 무시하는 효과가 있지만 그 노력에 감사드립니다. –

+0

예 아니요. 이전 제거 시도에서 남은 것입니다. 제거 할 수는 있지만 초기화를 위반하지 않고 컨트롤러를 만든 후에 배경색을 선택할 수 있습니다. 여기서는 무시하지 않습니다. 샘플 – poupou

+0

샘플을 편집하겠습니다. 어쨌든 요점은 어떤 것들은 특정 시간에만 할 수 있다는 것입니다. Console.WriteLine 행을 추가하면 사물이 호출 될 때 표시됩니다 (예 : '초기화'가 호출되지 않으면 주문이 달라지며 이는 두 샘플 간의 주요 차이점입니다). – poupou

관련 문제