Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
224 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...