Switching to different UITableViewControllers with UISegementedControl

江枫思渺然 提交于 2020-01-06 16:21:31

问题


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);
    }


来源:https://stackoverflow.com/questions/17234763/switching-to-different-uitableviewcontrollers-with-uisegementedcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!