在 iOS5 中使用 UISegmentedControl 切换 ViewController

发布于 2025-01-01 22:05:54 字数 2001 浏览 1 评论 0原文

我正在尝试一些非常简单的事情,但不知何故我无法让它发挥作用。我尝试做的就是使用 UISegmentedControl 在 2 个视图控制器之间切换,就像您可以在 App Store 应用程序的“突出显示”选项卡中看到的那样。

我正在使用 iOS5 和故事板。

这是我的 Storyboad 阵容:

在此处输入图像描述

所以我有一个根视图控制器和两个 UITableViews - 这 2 个 TableViews 我想要切换。

下面是实现文件的样子,

#import "SegmentedLocationViewController.h"
#import "PastEventsLocationViewController.h"
#import "FutureEventsLocationViewController.h"

@interface SegmentedLocationViewController()
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
@property (strong, nonatomic) NSArray *viewControllers;
@end


@implementation SegmentedLocationViewController

@synthesize segmentedControl = _segmentedControl;
@synthesize viewControllers = _viewControllers;

- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl*)segmentedControl
{
    NSLog(@"index: %d", segmentedControl.selectedSegmentIndex);
}

- (void)setupViewControllers
{
    PastEventsLocationViewController *pastEventsLocationViewController = [[PastEventsLocationViewController alloc] initWithStyle:UITableViewStylePlain];
    FutureEventsLocationViewController *futureEventsLocationViewController = [[FutureEventsLocationViewController alloc] initWithStyle:UITableViewStylePlain];

    self.viewControllers = [NSArray arrayWithObjects:pastEventsLocationViewController, futureEventsLocationViewController, nil];
}

- (void)setupUI
{
    [self.segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupViewControllers];
    [self setupUI];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end

我可以触发切换事件并可以记录当前选择的索引。但我不知道从这里该去哪里。

也许有人可以将我的注意力转向某个方向......?

I am trying something very simple but somehow I can't get it to work. All I try to do is switching between 2 View Controllers using an UISegmentedControl as you can see it for example in the App Store application in the Highlights tab.

I am using iOS5 and Storyboards.

Here's my Storyboad line up:

enter image description here

So I have a root View Controller and two UITableViews - This 2 TableViews I want to switch.

Here's how the implementation file looks like

#import "SegmentedLocationViewController.h"
#import "PastEventsLocationViewController.h"
#import "FutureEventsLocationViewController.h"

@interface SegmentedLocationViewController()
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
@property (strong, nonatomic) NSArray *viewControllers;
@end


@implementation SegmentedLocationViewController

@synthesize segmentedControl = _segmentedControl;
@synthesize viewControllers = _viewControllers;

- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl*)segmentedControl
{
    NSLog(@"index: %d", segmentedControl.selectedSegmentIndex);
}

- (void)setupViewControllers
{
    PastEventsLocationViewController *pastEventsLocationViewController = [[PastEventsLocationViewController alloc] initWithStyle:UITableViewStylePlain];
    FutureEventsLocationViewController *futureEventsLocationViewController = [[FutureEventsLocationViewController alloc] initWithStyle:UITableViewStylePlain];

    self.viewControllers = [NSArray arrayWithObjects:pastEventsLocationViewController, futureEventsLocationViewController, nil];
}

- (void)setupUI
{
    [self.segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupViewControllers];
    [self setupUI];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end

I can trigger the switch event and can log the currently selected index. But I don't have any idea where to go from here.

Maybe someone can turn my attention towards a certain direction...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

听,心雨的声音 2025-01-08 22:05:54

该代码非常适合您的目的,我将其用于我的一个新应用程序。
它使用新的 UIViewController 包含 API,允许 UIViewController 在您自己的 UIViewController 中,而无需手动转发诸如 viewDidAppear: 之类的东西,

- (void)viewDidLoad {
    [super viewDidLoad];
    // add viewController so you can switch them later. 
    UIViewController *vc = [self viewControllerForSegmentIndex:self.typeSegmentedControl.selectedSegmentIndex];
    [self addChildViewController:vc];
    vc.view.frame = self.contentView.bounds;
    [self.contentView addSubview:vc.view];
    self.currentViewController = vc;
}
- (IBAction)segmentChanged:(UISegmentedControl *)sender {
    UIViewController *vc = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
    [self addChildViewController:vc];
    [self transitionFromViewController:self.currentViewController toViewController:vc duration:0.5 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
        [self.currentViewController.view removeFromSuperview];
        vc.view.frame = self.contentView.bounds;
        [self.contentView addSubview:vc.view];
    } completion:^(BOOL finished) {
        [vc didMoveToParentViewController:self];
        [self.currentViewController removeFromParentViewController];
        self.currentViewController = vc;
    }];
    self.navigationItem.title = vc.title;
}

- (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
    UIViewController *vc;
    switch (index) {
        case 0:
            vc = [self.storyboard instantiateViewControllerWithIdentifier:@"FooViewController"];
            break;
        case 1:
            vc = [self.storyboard instantiateViewControllerWithIdentifier:@"BarViewController"];
            break;
    }
    return vc;
}

我从 Ray Wenderlichs 书的第 22 章中得到了这些东西 iOS5 教程
不幸的是,我没有教程的公共链接。但有一个 WWDC 2011 视频,标题为“实现 UIViewController Containment”

编辑

self.typeSegmentedControl 是您的 UISegmentedControl

self.contentView< /code> 是容器视图的出口

self.currentViewController 只是我们用来存储当前使用的 UIViewController 的属性

This code works pretty well for your purpose, I use it for one of my new apps.
It uses the new UIViewController containment APIs that allow UIViewControllers inside your own UIViewControllers without the hassles of manually forwarding stuff like viewDidAppear:

- (void)viewDidLoad {
    [super viewDidLoad];
    // add viewController so you can switch them later. 
    UIViewController *vc = [self viewControllerForSegmentIndex:self.typeSegmentedControl.selectedSegmentIndex];
    [self addChildViewController:vc];
    vc.view.frame = self.contentView.bounds;
    [self.contentView addSubview:vc.view];
    self.currentViewController = vc;
}
- (IBAction)segmentChanged:(UISegmentedControl *)sender {
    UIViewController *vc = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
    [self addChildViewController:vc];
    [self transitionFromViewController:self.currentViewController toViewController:vc duration:0.5 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
        [self.currentViewController.view removeFromSuperview];
        vc.view.frame = self.contentView.bounds;
        [self.contentView addSubview:vc.view];
    } completion:^(BOOL finished) {
        [vc didMoveToParentViewController:self];
        [self.currentViewController removeFromParentViewController];
        self.currentViewController = vc;
    }];
    self.navigationItem.title = vc.title;
}

- (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
    UIViewController *vc;
    switch (index) {
        case 0:
            vc = [self.storyboard instantiateViewControllerWithIdentifier:@"FooViewController"];
            break;
        case 1:
            vc = [self.storyboard instantiateViewControllerWithIdentifier:@"BarViewController"];
            break;
    }
    return vc;
}

I got this stuff from chapter 22 of Ray Wenderlichs book iOS5 by tutorial.
Unfortunately I don't have a public link to a tutorial. But there is a WWDC 2011 video titled "Implementing UIViewController Containment"

EDIT

self.typeSegmentedControl is outlet for your UISegmentedControl

self.contentView is outlet for your container view

self.currentViewController is just a property that we're using to store our currently used UIViewController

橙幽之幻 2025-01-08 22:05:54

这是 Matthias Bauch 的解决方案,但无论如何还是想在 Swift 中分享它!
编辑:
添加指向 Swift 2.0 现成演示应用程序的链接。
https://github.com/ahmed-abdurrahman/taby-segmented-control

var currentViewController: UIViewController?
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var segmentedControl: UISegmentedControl!

@IBAction func switchHappeningTabs(sender: UISegmentedControl) {
    if let vc = viewControllerForSelectedSegmentIndex(sender.selectedSegmentIndex) {
        self.addChildViewController(vc)
        self.transitionFromViewController(self.currentViewController!, toViewController: vc, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: {
            self.currentViewController!.view.removeFromSuperview()
            vc.view.frame = self.contentView.bounds
            self.contentView.addSubview(vc.view)
            }, completion: { finished in
                vc.didMoveToParentViewController(self)
                self.currentViewController!.removeFromParentViewController()
                self.currentViewController = vc
            }
        )        
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    if let vc = self.viewControllerForSelectedSegmentIndex(self.segmentedControl.selectedSegmentIndex) {
        self.addChildViewController(vc)
        self.contentView.addSubview(vc.view)
        self.currentViewController = vc
    }
}

func viewControllerForSelectedSegmentIndex(index: Int) -> UIViewController? {
    var vc: UIViewController?
    switch index {
    case 0:
            vc = self.storyboard?.instantiateViewControllerWithIdentifier("FooViewController") as? UIViewController
    case 1:
            vc = self.storyboard?.instantiateViewControllerWithIdentifier("BarViewController") as? UIViewController
    default:
        return nil
    }

    return vc
}

It's Matthias Bauch solution, but thought of sharing it in Swift anyway!
Edit:
Adding a link to a Swift 2.0 ready made demo app.
https://github.com/ahmed-abdurrahman/taby-segmented-control

var currentViewController: UIViewController?
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var segmentedControl: UISegmentedControl!

@IBAction func switchHappeningTabs(sender: UISegmentedControl) {
    if let vc = viewControllerForSelectedSegmentIndex(sender.selectedSegmentIndex) {
        self.addChildViewController(vc)
        self.transitionFromViewController(self.currentViewController!, toViewController: vc, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: {
            self.currentViewController!.view.removeFromSuperview()
            vc.view.frame = self.contentView.bounds
            self.contentView.addSubview(vc.view)
            }, completion: { finished in
                vc.didMoveToParentViewController(self)
                self.currentViewController!.removeFromParentViewController()
                self.currentViewController = vc
            }
        )        
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    if let vc = self.viewControllerForSelectedSegmentIndex(self.segmentedControl.selectedSegmentIndex) {
        self.addChildViewController(vc)
        self.contentView.addSubview(vc.view)
        self.currentViewController = vc
    }
}

func viewControllerForSelectedSegmentIndex(index: Int) -> UIViewController? {
    var vc: UIViewController?
    switch index {
    case 0:
            vc = self.storyboard?.instantiateViewControllerWithIdentifier("FooViewController") as? UIViewController
    case 1:
            vc = self.storyboard?.instantiateViewControllerWithIdentifier("BarViewController") as? UIViewController
    default:
        return nil
    }

    return vc
}
So尛奶瓶 2025-01-08 22:05:54

对于想要在快速

import UIKit

class HomeController: UIViewController {

    var currentViewController:UIViewController?

    @IBOutlet weak var homeController: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let initialController:UIViewController = self.viewControllerForSegmentedIndex(0)

        self.addChildViewController(initialController)

        initialController.view.frame = self.homeController.bounds
        self.homeController.addSubview(initialController.view)
        self.currentViewController = initialController

    }

    @IBAction func segmentChanged(sender: UISegmentedControl) {

        let viewCOntroller:UIViewController = viewControllerForSegmentedIndex(sender.selectedSegmentIndex)

        self.addChildViewController(viewCOntroller)

        self.transitionFromViewController(self.currentViewController!, toViewController: viewCOntroller, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromBottom, animations: {
            self.currentViewController?.view.removeFromSuperview()
            viewCOntroller.view.frame = self.homeController.bounds
            self.homeController.addSubview(viewCOntroller.view)

            }, completion:{ finished in

                viewCOntroller.didMoveToParentViewController(self)
                self.currentViewController?.removeFromParentViewController()
                self.currentViewController = viewCOntroller

        })

    }
    func viewControllerForSegmentedIndex(index:Int) -> UIViewController {
        var viewController:UIViewController?
        switch index {
        case 0:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardIdForFirstController")
            break
        case 1:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardIdForSecondController")
            break
        case 2:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardIdForThirdController")
            break
        default:
            break
        }
        return viewController!
    }
}

故事板视图

中实现相同功能的人在此输入图像描述

For someone want to implement the same in swift

import UIKit

class HomeController: UIViewController {

    var currentViewController:UIViewController?

    @IBOutlet weak var homeController: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let initialController:UIViewController = self.viewControllerForSegmentedIndex(0)

        self.addChildViewController(initialController)

        initialController.view.frame = self.homeController.bounds
        self.homeController.addSubview(initialController.view)
        self.currentViewController = initialController

    }

    @IBAction func segmentChanged(sender: UISegmentedControl) {

        let viewCOntroller:UIViewController = viewControllerForSegmentedIndex(sender.selectedSegmentIndex)

        self.addChildViewController(viewCOntroller)

        self.transitionFromViewController(self.currentViewController!, toViewController: viewCOntroller, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromBottom, animations: {
            self.currentViewController?.view.removeFromSuperview()
            viewCOntroller.view.frame = self.homeController.bounds
            self.homeController.addSubview(viewCOntroller.view)

            }, completion:{ finished in

                viewCOntroller.didMoveToParentViewController(self)
                self.currentViewController?.removeFromParentViewController()
                self.currentViewController = viewCOntroller

        })

    }
    func viewControllerForSegmentedIndex(index:Int) -> UIViewController {
        var viewController:UIViewController?
        switch index {
        case 0:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardIdForFirstController")
            break
        case 1:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardIdForSecondController")
            break
        case 2:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardIdForThirdController")
            break
        default:
            break
        }
        return viewController!
    }
}

Storyboard view

enter image description here

人生百味 2025-01-08 22:05:54

A是根视图控制器,B、C是子视图控制器。
当您单击segment时,添加子视图控制器。

结果

结果

设计

design

代码

import UIKit

class SegmentViewController: UIViewController {

    @IBOutlet weak var containerView: UIView!

    var leftViewController: LeftViewController!
    var rightViewController: RightViewController!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if let sb = storyboard {
            leftViewController = sb.instantiateViewControllerWithIdentifier("leftViewController") as! LeftViewController

            switchViewController(from: nil, to: leftViewController)
        } else {
            print("storyboard is nil")
        }
    }

    func switchViewController(from fromVC: UIViewController?, to toVC: UIViewController?) {
        if let from = fromVC {
            from.willMoveToParentViewController(nil)
            from.view.removeFromSuperview()
            from.removeFromParentViewController()
        } else {
            print("fromVC is nil")
        }

        if let to = toVC {
            self.addChildViewController(to)
            to.view.frame = CGRectMake(0, 0, containerView.frame.width, containerView.frame.height)
            self.containerView.insertSubview(to.view, atIndex: 0)
            to.didMoveToParentViewController(self)
        } else {
            print("toVC is nil")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

        removeViewController()
    }

    func removeViewController() {
        if let leftVC = leftViewController {
            if let _ = leftVC.parentViewController {
                print("leftVC is using")
            } else {
                print("set leftVC = nil")
                leftViewController = nil
            }
        }

        if let rightVC = rightViewController {
            if let _ = rightVC.parentViewController {
                print("rightVC is using")
            } else {
                print("set rightVC = nil")
                rightViewController = nil
            }
        }
    }


    @IBAction func onSegmentValueChanged(sender: UISegmentedControl) {
        UIView.beginAnimations("xxx", context: nil)
        UIView.setAnimationDuration(0.4)
        UIView.setAnimationCurve(.EaseInOut)

        switch sender.selectedSegmentIndex {
        case 0:
            UIView.setAnimationTransition(.FlipFromRight, forView: self.containerView, cache: true)

            if let leftVC = leftViewController {
                switchViewController(from: rightViewController, to: leftVC)
            } else {
                if let sb = storyboard {
                    leftViewController = sb.instantiateViewControllerWithIdentifier("leftViewController") as! LeftViewController
                    switchViewController(from: rightViewController, to: leftViewController)
                } else {
                    print("storyboard is nil")
                }
            }
        default:
            UIView.setAnimationTransition(.FlipFromLeft, forView: self.containerView, cache: true)

            if let rightVC = rightViewController {
                switchViewController(from: leftViewController, to: rightVC)
            } else {
                if let sb = storyboard {
                    rightViewController = sb.instantiateViewControllerWithIdentifier("rightViewController") as! RightViewController
                    switchViewController(from: leftViewController, to: rightViewController)
                } else {
                    print("storyboard is nil")
                }
            }
        }

        UIView.commitAnimations()
    }
}

A is root view controller, and B, C is sub view controller.
When you click segment, add sub view controller.

result

result

design

design

code

import UIKit

class SegmentViewController: UIViewController {

    @IBOutlet weak var containerView: UIView!

    var leftViewController: LeftViewController!
    var rightViewController: RightViewController!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if let sb = storyboard {
            leftViewController = sb.instantiateViewControllerWithIdentifier("leftViewController") as! LeftViewController

            switchViewController(from: nil, to: leftViewController)
        } else {
            print("storyboard is nil")
        }
    }

    func switchViewController(from fromVC: UIViewController?, to toVC: UIViewController?) {
        if let from = fromVC {
            from.willMoveToParentViewController(nil)
            from.view.removeFromSuperview()
            from.removeFromParentViewController()
        } else {
            print("fromVC is nil")
        }

        if let to = toVC {
            self.addChildViewController(to)
            to.view.frame = CGRectMake(0, 0, containerView.frame.width, containerView.frame.height)
            self.containerView.insertSubview(to.view, atIndex: 0)
            to.didMoveToParentViewController(self)
        } else {
            print("toVC is nil")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

        removeViewController()
    }

    func removeViewController() {
        if let leftVC = leftViewController {
            if let _ = leftVC.parentViewController {
                print("leftVC is using")
            } else {
                print("set leftVC = nil")
                leftViewController = nil
            }
        }

        if let rightVC = rightViewController {
            if let _ = rightVC.parentViewController {
                print("rightVC is using")
            } else {
                print("set rightVC = nil")
                rightViewController = nil
            }
        }
    }


    @IBAction func onSegmentValueChanged(sender: UISegmentedControl) {
        UIView.beginAnimations("xxx", context: nil)
        UIView.setAnimationDuration(0.4)
        UIView.setAnimationCurve(.EaseInOut)

        switch sender.selectedSegmentIndex {
        case 0:
            UIView.setAnimationTransition(.FlipFromRight, forView: self.containerView, cache: true)

            if let leftVC = leftViewController {
                switchViewController(from: rightViewController, to: leftVC)
            } else {
                if let sb = storyboard {
                    leftViewController = sb.instantiateViewControllerWithIdentifier("leftViewController") as! LeftViewController
                    switchViewController(from: rightViewController, to: leftViewController)
                } else {
                    print("storyboard is nil")
                }
            }
        default:
            UIView.setAnimationTransition(.FlipFromLeft, forView: self.containerView, cache: true)

            if let rightVC = rightViewController {
                switchViewController(from: leftViewController, to: rightVC)
            } else {
                if let sb = storyboard {
                    rightViewController = sb.instantiateViewControllerWithIdentifier("rightViewController") as! RightViewController
                    switchViewController(from: leftViewController, to: rightViewController)
                } else {
                    print("storyboard is nil")
                }
            }
        }

        UIView.commitAnimations()
    }
}
悲念泪 2025-01-08 22:05:54

您可以将初始视图控制器嵌入到导航控制器中。通过在故事板中选择视图控制器,然后选择编辑器 -> 嵌入 -> 导航控制器来完成此操作。

在你的indexDidChangeForSegmentedControl:方法中,你只需将相应的视图控制器推送到导航堆栈:

- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl*)segmentedControl
{
    [self.navigationController pushViewController:[self.viewControllers objectAtIndex:[segmentedControl.selectedIndex]] animated:YES];

}

但是当你使用故事板时,你的方法根本没有多大意义。
我不知道是否可以使用 Segues 将单个分段控制按钮连接到视图控制器。试试吧。

You could embed your initial view controller in a navigation controller. Do that by selecting the view controller in the storyboard, then Editor->Embed in->Navigation Controller.

In your indexDidChangeForSegmentedControl: method you simply push the corresponding view controller to the navigation stack:

- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl*)segmentedControl
{
    [self.navigationController pushViewController:[self.viewControllers objectAtIndex:[segmentedControl.selectedIndex]] animated:YES];

}

But your approach makes not too much sense at all when you are using storyboards.
I don't know whether you can wire up a single segmented control button to a view controller using segues. Just try it.

彡翼 2025-01-08 22:05:54

我最终使用导航控制器来实现类似的功能。

import UIKit

class BaseViewController: UIViewController {

  var currentSegmentIndex = 0

  @IBOutlet weak var segmentedControl: UISegmentedControl!

  override func viewDidLoad() {
    super.viewDidLoad()

    segmentedControl.selectedSegmentIndex = currentSegmentIndex
  }

  @IBAction func segmentedControlChanged(_ sender: Any) {
    let idx = segmentedControl.selectedSegmentIndex
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    switch idx {
    case 0:
      let vc = storyboard.instantiateViewController(
        withIdentifier: "Foo") as! FooViewController
      vc.currentSegmentIndex = 0
      navigationController?.viewControllers = [vc]
      break
    case 1:
      let vc = storyboard.instantiateViewController(
        withIdentifier: "Bar") as! BarViewController
      vc.currentSegmentIndex = 1
      navigationController?.viewControllers = [vc]
      break
    default:
      break
    }
  }
}

I ended up using a navigation controller to achieve a similar functionality.

import UIKit

class BaseViewController: UIViewController {

  var currentSegmentIndex = 0

  @IBOutlet weak var segmentedControl: UISegmentedControl!

  override func viewDidLoad() {
    super.viewDidLoad()

    segmentedControl.selectedSegmentIndex = currentSegmentIndex
  }

  @IBAction func segmentedControlChanged(_ sender: Any) {
    let idx = segmentedControl.selectedSegmentIndex
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    switch idx {
    case 0:
      let vc = storyboard.instantiateViewController(
        withIdentifier: "Foo") as! FooViewController
      vc.currentSegmentIndex = 0
      navigationController?.viewControllers = [vc]
      break
    case 1:
      let vc = storyboard.instantiateViewController(
        withIdentifier: "Bar") as! BarViewController
      vc.currentSegmentIndex = 1
      navigationController?.viewControllers = [vc]
      break
    default:
      break
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文