Switching to different UITableViewControllers with

2019-08-28 20:43发布

问题:

I've an UINavigationViewController with an UISegmentedControl in the navigation bar. I want to achieve a simple switching to different ViewControllers when users push on the segmented control.

I've tried a lot and nothing works... Considered sources:

  • MonoTouch Instantiating a ViewController programmatically for ContainerView
  • https://stackoverflow.com/search?q=viewcontroller+intptr+handle
  • And a lot of google research...

The whole project is storyboard based! Any solutions which targets NIB's aren't useful.

Adding a ContainerControl to my UINavigationViewController. But in this case I can only embed one controller. Creating a Embedded-Segue programmatically was not possible. Even more instantiating a UITableViewController in code which is designed in IB results in an empty view. Because I've to change the c'tor from MyTableViewController(IntPtr handle) : base(handle) to an empty constructor.

Can someone publish a working example how to use a UISegmentedControl to switch between different ViewControllers? I appreciate all your help very much.

回答1:

Working solution:

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

        CreateAndEmbed (TrDetailNavType.Info);
    }

    partial void segmentNavigationValueChanged (MonoTouch.UIKit.UISegmentedControl sender, MonoTouch.UIKit.UIEvent e)
    {
        CreateAndEmbed((TrDetailNavType)sender.SelectedSegment);
    }

    private void CreateAndEmbed(TrDetailNavType tab)
    {
        if (_currentViewController != null) 
        {
            _currentViewController.View.RemoveFromSuperview ();
            _currentViewController.RemoveFromParentViewController();
        }

        string id;
        switch (tab)
        {
        case TrDetailNavType.Info:
            id = "TagesRapportDetailInfoTableViewController";
            break;
        case TrDetailNavType.Lohn:
        case TrDetailNavType.Material:
        case TrDetailNavType.Inventar:
        case TrDetailNavType.Fremdleistung:
        case TrDetailNavType.Regie:
            id = "TagesRapportDetailDummyViewController";
            break;
        }

        _currentViewController = (UIViewController)Storyboard.InstantiateViewController (id);
        _currentViewController.View.Frame = containerDetail.Bounds;
        AddChildViewController (_currentViewController);
        _currentViewController.DidMoveToParentViewController (this);
        containerDetail.AddSubview (_currentViewController.View);
    }