【发布时间】:2012-02-25 00:05:00
【问题描述】:
我正在尝试一些非常简单的方法,但不知何故我无法让它工作。我所要做的就是使用 UISegmentedControl 在 2 个视图控制器之间切换,例如,您可以在 App Store 应用程序的 Highlights 选项卡中看到它。
我正在使用 iOS5 和 Storyboards。
这是我的故事板系列:
所以我有一个根视图控制器和两个 UITableViews - 这两个 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
我可以触发 switch 事件并且可以记录当前选择的索引。但我不知道从这里去哪里。
也许有人可以将我的注意力转向某个方向...?
【问题讨论】:
-
你有没有想过只用不同的值重新加载 tableView?
-
你是对的。我现在会检查一下。我可能觉得太复杂了……
标签: ios uiviewcontroller uikit uisegmentedcontrol