本文主要是介绍IOS5.0中UIControllerView新方法的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在以前,一个UIViewController的View可能有很多小的子view。这些子view很多时候被盖在最后,我们在最外层ViewController的viewDidLoad方法中,用addSubview增加了大量的子view。这些子view大多数不会一直处于界面上,只是在某些情况下才会出现,例如登陆失败的提示view,上传附件成功的提示view,网络失败的提示view等。但是虽然这些view很少出现,但是我们却常常一直把它们放在内存中。另外,当收到内存警告时,我们只能自己手工把这些view从super view中去掉。
@property(nonatomic,readonly) NSArray *childViewControllers
- (void)addChildViewController:(UIViewController *)childController
- (void) removeFromParentViewCont
- (void)transitionFromViewContro
- (void)willMoveToParentViewCont
- (void)didMoveToParentViewContr
[父视图控制器 addChildViewController:子视图控制器];
- (void)addChildViewController:(UIViewController *)childController
向视图控制器容器中添加子视图控制器
childController:子视图控制器
当要添加的子视图控制器已经包含在视图控制器容器中,那么,相当于先从父视图控制器中删除,然后重新添加到父视图控制器中。
- (void)removeFromParentViewCont
从父视图控制器中删除。
- (void)transitionFromViewContro
交换两个子视图控制器的位置(由于添加的顺序不同,所以子试图控制器在父视图控制器中存在层次关系)
fromViewController:当前显示的子试图控制器,将被替换为非显示状态
toViewController:将要显示的子视图控制器
duration:交换动画持续的时间,单位秒
options:动画的方式
animations:动画Block
completion:完成后执行的Block
- (void)willMoveToParentViewCont
当一个视图控制器从视图控制器容器中被添加或者被删除之前,该方法被调用
parent:父视图控制器,如果没有父视图控制器,将为nil
注意点:
1.当我们向我们的视图控制器容器中调用removeFromParentViewCont
[将要删除的视图控制器
2.当我们调用addChildViewController方法时,在添加子视图控制器之前将自动调用该方法。所以,就不需要我们显示调用了。
- (void)didMoveToParentViewContr
当从一个视图控制容器中添加或者移除viewController后,该方法被调用。
parent:父视图控制器,如果没有父视图控制器,将为nil
当我们向我们的视图控制器容器(就是父视图控制器,它调用addChildViewController方法加入子视图控制器,它就成为了视图控制器的容器)中添加(或者删除)子视图控制器后,必须调用该方法,告诉iOS,已经完成添加(或删除)子控制器的操作。
removeFromParentViewCont
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, weak) UIView *nav;
@property (nonatomic, weak) UILabel *navTitle;
@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, strong) UIViewController *currentController;
@end
#import "ViewController.h"
#import "firseViewController.h"
#import "secondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *vBar = [[UIView alloc] initWithFrame:CGRectMake(-1, -1, self.view.frame.size.width+2, 44)];
vBar.layer.shadowColor = [UIColor blackColor].CGColor;
vBar.layer.shadowOffset = CGSizeMake(0, 1);
vBar.layer.shadowOpacity = 1;
vBar.layer.shadowRadius = 4;
vBar.layer.borderWidth = 1;
vBar.layer.borderColor = (__bridge CGColorRef)([[UIColor blackColor] colorWithAlphaComponent:0.4]);
// vBar.borderWidth = 1;
// vBar.borderColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
vBar.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
self.nav = vBar;
float x = self.view.frame.size.width/3;
UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, x, 44)];
lb.font = [UIFont boldSystemFontOfSize:18.0f];
lb.textAlignment = NSTextAlignmentCenter;
lb.textColor = [UIColor whiteColor];
lb.text = @"make";
lb.backgroundColor = [UIColor clearColor];
self.navTitle = lb;
UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 400)];
cView.backgroundColor = [UIColor greenColor];
cView.clipsToBounds = YES;
self.contentView = cView;
[vBar addSubview:lb];
[self.view addSubview:vBar];
[self.view addSubview:cView];
UIButton *pbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pbtn.tag = 0;
[pbtn setTitle:@"changCurrentView" forState:UIControlStateNormal];
[pbtn addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
[pbtn setFrame: CGRectMake(80, 430, 200, 50)];
[self.view addSubview:pbtn];
firseViewController *view = [[firseViewController alloc] init];
view.baseView = self;
self.currentController = view;
[self.contentView addSubview:view.view];
[self addChildViewController:view];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)press:(UIButton *)sender
{
firseViewController *first =[[firseViewController alloc] init];
secondViewController *second = [[secondViewController alloc] init];
int i = sender.tag;
UIViewController *toControl;
switch (i) {
case 0:
//second.baseView = self;
[self addChildViewController:second];
toControl = second;
second.baseView.navTitle.text = @"Second";
sender.tag = 1;
break;
case 1:
//first.baseView = self;
[self addChildViewController:first];
toControl = first;
sender.tag = 0;
first.baseView.navTitle.text = @"First";
break;
default:
break;
}
//将要把视图从视图控制器中移除
[self.currentController willMoveToParentViewController:nil];
//交换两个视图控制器的位置
[self transitionFromViewController:self.currentController toViewController:toControl duration:0.3 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
} completion:^(BOOL finished) {
[self.currentController removeFromParentViewController];
[toControl didMoveToParentViewController:self];
self.currentController = toControl;
}];
}
- (void)willMoveToParentViewController:(UIViewController *)parent
{
NSLog(@"willMoveToParentViewController:%@",parent);
}
- (void)didMoveToParentViewController:(UIViewController *)parent
{
NSLog(@"didMoveToParentViewController:%@",parent);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这篇关于IOS5.0中UIControllerView新方法的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!