Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

xamarin.ios - Switching to different UITableViewControllers with UISegementedControl

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...