我正在尝试一些非常简单的操作,但是不知何故它无法正常工作。我的目标只是使用 UISegmentedControl 在两个视图控制器之间进行切换,就像在应用商店应用程序的“亮点”选项卡中所看到的。
我正在使用 iOS5 和 Storyboards。
这是我的故事板布局:
所以我有一个根视图控制器和两个 UITableViews - 我想要在这两个 TableView 之间切换。
这是实现文件的样子
#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
我可以触发开关事件并记录当前选定的索引。但是我不知道接下来该怎么做。
也许有人可以指引我朝着某个方向前进...?