只是为了让您了解视图控制器包含可能是什么样子(iOS 5),这是一种方法。它由四个类组成,容器视图控制器(其视图将具有用于在两个子视图控制器之间切换的分段控制)、两个随机子视图控制器和一个我们将存储数据的模型类(可以通过两个子视图控制器)。
首先,您使用分段控件创建一个容器视图控制器(我还添加了一个 UIView,它基本上定义了放置子视图控制器视图的框架,只是为了更容易弄清楚该视图的放置位置):
// ContainerViewController.h
#import <UIKit/UIKit.h>
@interface ContainerViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
@property (weak, nonatomic) IBOutlet UIView *childView;
- (IBAction)changeChild:(id)sender;
@end
然后你实现它:
// ContainerViewController.m
#import "ContainerViewController.h"
#import "FirstContainedViewController.h"
#import "SecondContainedViewController.h"
#import "MyModel.h"
@interface ContainerViewController ()
{
FirstContainedViewController *_controller0;
SecondContainedViewController *_controller1;
MyModel *_model;
UIViewController __weak *_currentChildController; // let's keep track of the current
}
@end
@implementation ContainerViewController
@synthesize segmentedControl = _segmentedControl;
@synthesize childView = _childView;
- (void)dealloc
{
// let's release our child controllers
_controller0 = nil;
_controller1 = nil;
// and release the model, too
_model = nil;
}
// this is my own method to
// 1. add the child view controller to the view controller hierarchy;
// 2. do the appropriate notification (even though I don't use it, Apple says you should do this, so I will); and
// 3. set the frame size to the appropriate size
- (void)addChildToThisContainerViewController:(UIViewController *)childController
{
[self addChildViewController:childController];
[childController didMoveToParentViewController:self];
childController.view.frame = CGRectMake(0.0,
0.0,
self.childView.frame.size.width,
self.childView.frame.size.height);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// let's create our model, our data
_model = [[MyModel alloc] init];
// set the segmented index to point to the first one
[self.segmentedControl setSelectedSegmentIndex:0];
// let's create our two controllers and provide each a pointer to our model
_controller0 = [[FirstContainedViewController alloc] initWithNibName:@"FirstContainedView" bundle:nil];
_controller0.model = _model;
_controller1 = [[SecondContainedViewController alloc] initWithNibName:@"SecondContainedView" bundle:nil];
_controller1.model = _model;
// let's add them to the view controller hierarchy
[self addChildToThisContainerViewController:_controller0];
[self addChildToThisContainerViewController:_controller1];
// let's add the currently selected controller as the "current child controller" and add it to our current view
_currentChildController = [self.childViewControllers objectAtIndex:self.segmentedControl.selectedSegmentIndex];
[self.childView addSubview:_currentChildController.view];
}
- (void)viewDidUnload
{
[self setChildView:nil];
[self setSegmentedControl:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (IBAction)segmentedControlValueChanged:(UISegmentedControl *)sender
{
UIViewController *oldChildController = _currentChildController;
UIViewController *newChildController = [self.childViewControllers objectAtIndex:sender.selectedSegmentIndex];
UIViewAnimationOptions options;
// let's change the animation based upon which segmented control you select ... you may change this as fits your desired UI
if (sender.selectedSegmentIndex == 0)
options = UIViewAnimationOptionTransitionFlipFromLeft;
else
options = UIViewAnimationOptionTransitionFlipFromRight;
[self transitionFromViewController:oldChildController
toViewController:newChildController
duration:0.5
options:options
animations:nil
completion:nil];
_currentChildController = newChildController;
}
@end
我的模型只有两个数据元素、一个对象数组和一个字符串,但显然你可以为所欲为。我将只显示标题(因为实现细节微不足道且无趣):
// MyModel.h
#import <Foundation/Foundation.h>
@interface MyModel : NSObject
@property (nonatomic, strong) NSMutableArray *listOfItems;
@property (nonatomic, strong) NSString *displayText;
- (MyModel *)init;
@end
子视图控制器同样简单:
// FirstContainedViewController.h
#import <UIKit/UIKit.h>
@class MyModel;
@interface FirstContainedViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, weak) MyModel *model;
@end
实现可能看起来像(这是一个简单的示例,但显示了如何从共享模型中检索信息):
// FirstContainedViewController.m
#import "FirstContainedViewController.h"
#import "MyModel.h"
@implementation FirstContainedViewController
@synthesize model = _model;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - tableview data source delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.model.listOfItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"fcvc";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.textLabel.text = [self.model.listOfItems objectAtIndex:indexPath.row];
return cell;
}
@end
希望这能让您了解如何为两个视图使用单独的视图控制器,如何在它们之间切换,使您的模型可供两者访问。这是一个相当简单的例子,但它很实用。我可能会建议一些优化,但希望这足以让你朝着正确的方向前进。