1

我試圖創建一個UITabBarController實現UINavigationBar,我會在幾分鐘內在Xcode中做的事情。但是,我正在努力使用MonoTouch的MVVMCross。一些代碼低於 -MVVMCross MonoTouch UITabBarController無法訪問導航欄

從第一個VC(這是爲用戶接受條款&條件,所以一旦被接受,沒有選擇回到它,因此真正的標誌) -

this.RequestNavigate<TabHostViewModel>(true); 

我的TabBar設置像這樣,這工作正常 -

ViewControllers = new UIViewController[] 
{ 
    CreateTabFor("Home", "", ViewModel.homeViewModel), 
    CreateTabFor("History", "", ViewModel.journeyHistoryViewModel), 
    CreateTabFor("Contacts", "", ViewModel.contactsViewModel), 
    CreateTabFor("About", "", ViewModel.aboutViewModel), 
}; 

...等。

我嘗試設置(在這種情況下HomeView)的第一個視圖像這樣在viewDidLoad中 -

this.NavigationController.NavigationBar.TintColor = myNavBarColour; 

然而,似乎NavigationController不確定的,除非我創造我自己,當我設置的TabBar -

UIViewController HomeViewController = CreateTabFor("Home", "", ViewModel.homeViewModel); 
UINavigationController HomeNavController = new UINavigationController(HomeViewController); 

ViewControllers = new UIViewController[] 
{ 
    HomeNavController, 
    CreateTabFor("History", "", ViewModel.journeyHistoryViewModel), 
    CreateTabFor("Contacts", "", ViewModel.contactsViewModel), 
    CreateTabFor("About", "", ViewModel.aboutViewModel), 
}; 

現在,我可以做任何我與導航欄一樣,但麻煩的是我有兩個導航欄,一個在沒有標題的上面,我剛剛立即創建它下面的一個新。

任何有任何想法的人?

非常感謝。

+0

我不完全清楚你的設置。但是,我認爲你應該*不在家裏有一個導航欄 - 相反,你應該在每個標籤中有單獨的導航欄。這是會議樣本的功能 - http://slodge.blogspot.co.uk/2012/03/update-on-mvvmcross-sqlbits-conference.html和https://github.com/slodge/MvvmCross/tree/主/樣本%20-%20CirriousConference – Stuart

+0

感謝斯圖爾特 - 我現在正在通過代碼的方式。不幸的是,該應用程序無法運行,它在UpdateView中的base.ViewDidLoad()上崩潰。 – SomaMan

+0

嗯...什麼版本的MonoTouch ...它確實運行了......甚至在應用商店中...... argh – Stuart

回答

0

我不完全清楚你的設置。

不過,我認爲你正在尋找的是什麼樣的會議樣品確實

在這個示例:

  • 主視圖主機標籤欄
  • 每個標籤都有它自己的嵌入式NavigationController
  • 每個子視圖的內部的導航控制器

該代碼是一個託管:

private UIViewController CreateTabFor(string title, string imageName, IMvxViewModel viewModel) 
    { 
     var controller = new UINavigationController(); 
     controller.NavigationBar.TintColor = UIColor.Black; 
     var screen = this.CreateViewControllerFor(viewModel) as UIViewController; 
     SetTitleAndTabBarItem(screen, title, imageName); 
     controller.PushViewController(screen, false); 
     return controller; 
    } 

    private void SetTitleAndTabBarItem(UIViewController screen, string title, string imageName) 
    { 
     screen.Title = ViewModel.TextSource.GetText(title); 
     screen.TabBarItem = new UITabBarItem(title, UIImage.FromBundle("Images/Tabs/" + imageName + ".png"), 
              _createdSoFarCount); 
     _createdSoFarCount++; 
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 

     if (ViewModel == null) 
     { 
      _needViewDidLoadCall = true; 
      return; 
     } 

     _needViewDidLoadCall = false; 

     var viewControllers = new UIViewController[] 
           { 
           CreateTabFor("Welcome", "home", ViewModel.Welcome), 
           CreateTabFor("Sessions", "sessions", ViewModel.Sessions), 
           CreateTabFor("Favorites", "favorites", ViewModel.Favorites), 
           CreateTabFor("Tweets", "twitter", ViewModel.Twitter), 
           }; 
     ViewControllers = viewControllers; 
     CustomizableViewControllers = new UIViewController[] { }; 
     SelectedViewController = ViewControllers[0]; 
    } 

然後將導航演示邏輯內截獲(在被配置所述的AppDelegate) - 的ConferencePresenter推遲的ShowView邏輯:

public bool ShowView(IMvxTouchView view) 
    { 
     if (TryShowViewInCurrentTab(view)) 
      return true; 

     return false; 
    } 

    private bool TryShowViewInCurrentTab(IMvxTouchView view) 
    { 
     var navigationController = (UINavigationController)this.SelectedViewController; 
     navigationController.PushViewController((UIViewController)view, true); 
     return true; 
    } 

此邏輯有點複雜,但此導航和演示者設計的目的是允許您自定義演示以適應應用程序。這也可以在運行時動態更改 - 例如,您可以選擇在iPad上使用不同的表示邏輯而不是iPhone。