只要你得到一個什麼樣的視圖控制器遏制可能看起來像感(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
希望這給你的,你怎麼可能使用不同的看法感兩個視圖的控制器,如何在它們之間切換,使您的模型都可以訪問。這是一個相當簡單的例子,但它是功能性的。我可能會提出一些優化建議,但希望這足以讓您朝着正確的方向前進。
我從來沒有使用這種方法的單獨的類爲tableViews委託或數據源,總是父視圖控制器。如果我這樣做,我在哪裏保持我的模型?它會在viewController中持有tableView的實例,還是在datasource委託類中? – Eyal
MVC純粹主義者會告訴你控制器應該管理模型。在實踐中,我可以將對模型的引用傳遞給委託和數據源(所以來自委託self.listModel),而不是通過控制器引用它(如self.owningController.listModel)。從技術上講,你既可以做到這一點,也可以嘗試兩種方式,看看你感覺更好。注意保留週期。 –