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

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

问题描述

我正在尝试一些非常简单的事情,但不知怎的,我无法让它发挥作用。我尝试做的就是使用UISegmentedControl在2个View控制器之间切换,例如你可以在Highlights选项卡的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和故事板。

I am using iOS5 and Storyboards.

这是我的Storyboad阵容:

Here's my Storyboad line up:

所以我有一个根视图控制器和两个UITableViews - 这2个我想要的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

我可以触发切换事件并可以记录当前选定的索引。但是我不知道从哪里开始。

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包含允许在您自己的UIViewControllers中使用UIViewControllers的API,无需手动转发诸如 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 Wenderlichs的第22章 iOS5教程
很遗憾,我没有指向教程的公共链接。但是有一个WWDC 2011视频标题为实施UIViewController遏制

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切换ViewControllers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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