在 iOS5 中使用 UISegmentedControl 切换 ViewController [英] Switching ViewControllers with UISegmentedControl in iOS5

查看:21
本文介绍了在 iOS5 中使用 UISegmentedControl 切换 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些非常简单的事情,但不知何故我无法让它工作.我尝试做的就是使用 UISegmentedControl 在 2 个视图控制器之间切换,例如您可以在 App Store 应用程序的亮点"选项卡中看到它.

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.

我使用的是 iOS5 和 Storyboard.

I am using iOS5 and Storyboards.

这是我的故事板阵容:

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

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

这是实现文件的样子

#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

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

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...?

推荐答案

此代码非常适合您的用途,我将其用于我的一个新应用.
它使用新的 UIViewController 包含 API,允许在您自己的 UIViewController 中使用 UIViewController,而无需手动转发诸如 viewDidAppear:

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

我从 Ray Wenderlich 的书 iOS5 教程的第 22 章中得到了这些内容.不幸的是,我没有教程的公共链接.但是有一个 WWDC 2011 视频,标题是Implementing UIViewController Containment"

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"

编辑

self.typeSegmentedControl 是你的 UISegmentedControl

self.contentView 是容器视图的出口

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

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

这篇关于在 iOS5 中使用 UISegmentedControl 切换 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆