简体   繁体   中英

Xamarin.iOS MvvmCross, back navigation button and tabs

I doing an application with various views types : MvxViewController, MvxTabBarViewController, ...

But when I want to do it, I meet difficulties : Following initial directives ( http://bit.ly/1hLNMF3 , http://bit.ly/1hNNY2g ), I loose navigation back button among others.

So, I want to mix simple views and tabbed view without loosing Back button and without recode it (with NavigationItem.SetLeftBarButtonItem : http://bit.ly/1fsqGEC ). Inspired by these solutions, I've do this :

  • A list of items (simple MvxTableController)
  • A detailed view of my items - MvxTabBarViewController, who have 3 tabs (1 view attached to each tab)

For the MvxTabBarViewController - main controller for item details :

public partial class SecondView : MvxTabBarViewController {
    private int _count = 0;

    public SecondView() {
        ViewDidLoad();
    }

    public new SecondViewModel ViewModel {
        get { return (SecondViewModel)base.ViewModel; }
        set { base.ViewModel = value; }
    }

    public override void ViewDidLoad() {
        base.ViewDidLoad();
        if (ViewModel == null) return;
        var viewControllers = new UIViewController[] {
            CreateTabFor ("tab 1", "t1", ViewModel.Tab1);
            CreateTabFor ("tab 2", "t2", ViewModel.Tab2);
            CreateTabFor ("tab 3", "t3", ViewModel.Tab3);
        }
        ViewControllers = viewControllers;
        CustomizableViewControllers = new UIViewController[0] { }
        SelectedViewController = ViewControllers [0]
    }

    private UIViewController CreateTabFor (string tabTitle, string tabImage, IMvxViewModel viewModel) {
        var controller = new UITabViewController (); 
        var screen = this.CreateViewControllerFor(viewModel) as UIViewController;
        controller.TabBarItem = new UITabBarItem (tabTitle, UIImage.FromBundle("Images/" + tabImage + ".png"), _count);
        _count++;
        controller.Add (screen.View);
        return controller;
    }
}

Moreover, I don't need to change Setup class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM