ios - 在 iOS5 中使用 UISegmentedControl 切换 ViewController

标签 ios uiviewcontroller uikit uisegmentedcontrol

我正在尝试一些非常简单的事情,但不知何故我无法让它工作。我尝试做的就是使用 UISegmentedControl 在 2 个 View Controller 之间切换,您可以在 App Store 应用程序的“亮点”选项卡中看到它。

我正在使用 iOS5 和 Storyboard。

这是我的 Storyboad 系列:

enter image description here

所以我有一个 Root View Controller 和两个 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

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

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

最佳答案

此代码非常适合您的用途,我将它用于我的一个新应用程序。
它使用新的 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 Wenderlich 的书 iOS5 by tutorial 的第 22 章得到了这些东西. 不幸的是,我没有指向教程的公共(public)链接。但是有一个 WWDC 2011 视频标题为“实现 UIViewController 包含”

编辑

self.typeSegmentedControl 是您的 UISegmentedControl

的导出

self.contentView 是容器 View 的导出

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

的属性

关于ios - 在 iOS5 中使用 UISegmentedControl 切换 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9110567/

相关文章:

ios - Swift 中一个屏幕上两个 UIViewController 之间的交互

ios - UIVideoAtPathIsCompatibleWithSavedPhotosAlbum 找不到文件

iphone - UIButton 无 - IOS

ios - 查看旧导航栏和选项卡栏 Controller 的 Controller 问题

ios - UITableViewCell 在运行时看起来与 InterfaceBuilder 中不同(在 iPad 上)

ios - 为菜单重用静态 UIViewController 代码的正确方法

ios - 从内存中删除未使用的 ViewController

ios - 拖放适用于没有 UITableViewDropDelegate 的 UITableView

iOS 11 及更高版本崩溃 -[UIView setDrawsWithVibrantLightMode :]: unrecognized selector sent to instance

ios - 类型 'MapViewController' 没有成员 'mapTypeChanged' Swift Big Nerd Ranch Guide