IOS5中的新增加对UIViewController的子控制器操作方法

来源:互联网 发布:薛之谦专辑签名淘宝 编辑:程序博客网 时间:2024/04/28 17:28

IOS5之后,UIViewController增加了一些操作子控制器的方法,以下列举几个:

添加子控制器:

- (void)addChildViewController:(UIViewController *)childController NS_AVAILABLE_IOS(5_0);
移除子控制器:
- (void) removeFromParentViewController NS_AVAILABLE_IOS(5_0);
改变控制器从fromViewController到toViewController,过程几秒,动画选项,自定义动画方法,完成方法
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);

下面是一个通过childViewController来改变视图的demo:

主视图控制器 :laomaoViewController.h

#import <UIKit/UIKit.h>#import "laomaoFirstViewController.h"#import "laomaoSecondViewController.h"@interface laomaoViewController : UIViewController{    laomaoFirstViewController * _firstViewController;    laomaoSecondViewController * _secondViewController;    UIViewController * _currentViewController;}@property (retain, nonatomic) IBOutlet UIView *contentView;@end

主视图控制器实现方法:laomaoViewController.m

#import "laomaoViewController.h"@interface laomaoViewController ()@end@implementation laomaoViewController- (void)viewDidLoad{    [super viewDidLoad];_firstViewController = [[laomaoFirstViewController alloc]init];    [self addChildViewController:_firstViewController];    _secondViewController = [[laomaoSecondViewController alloc]init];    [self addChildViewController:_secondViewController];    //[self.view addSubview:_firstViewController.view];    [self.contentView addSubview:_firstViewController.view];    _currentViewController = _firstViewController;    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 140, 40)];    button.backgroundColor = [UIColor grayColor];    [button addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];    [button setTitle:@"clickChangeView" forState:UIControlStateNormal];    [self.view addSubview:button];}- (void)changeView {    [self transitionFromViewController:_currentViewController == _firstViewController?_firstViewController:_secondViewController toViewController:_currentViewController == _firstViewController?_secondViewController:_firstViewController duration:2 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{    } completion:^(BOOL finished){        _currentViewController = _currentViewController == _firstViewController?_secondViewController:_firstViewController;    }];}- (void)viewDidUnload {    NSLog(@"ViewController----ViewDidUnload");}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    NSLog(@"ViewController----didReceiveMemoryWarning");    // Dispose of any resources that can be recreated.}- (void)dealloc {    [_contentView release];    [super dealloc];}@end

第一个子视图控制器:laomaoFirstViewController.h

#import <UIKit/UIKit.h>@interface laomaoFirstViewController : UIViewController@end

第一个子视图控制实现文件:laomaoFirstViewController.m

#import "laomaoFirstViewController.h"@interface laomaoFirstViewController ()@end@implementation laomaoFirstViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        self.view.frame = CGRectMake(40, 0, 240, 240);    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];self.view.backgroundColor = [UIColor blueColor];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    NSLog(@"FirstViewController----didReceiveMemoryWarning");}@end

第二个子视图控制器:laomaoSecondViewController.h

#import <UIKit/UIKit.h>@interface laomaoSecondViewController : UIViewController@end

第二个子视图控制器实现文件:laomaoSecondViewController.m

#import "laomaoSecondViewController.h"@interface laomaoSecondViewController ()@end@implementation laomaoSecondViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        self.view.frame = CGRectMake(40, 0, 240, 240);    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];self.view.backgroundColor = [UIColor redColor];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    NSLog(@"SecondViewController----didReceiveMemoryWarning");}@end


原创粉丝点击