调用
loadView
方法一定要给自己一个
View
调用loadView方法需要满足的条件:
1 、 view 属性的 get 方法被调用的时候
2 、 view 为空的时候
调用loadView方法需要满足的条件:
1 、 view 属性的 get 方法被调用的时候
2 、 view 为空的时候
只有当以上两个条件同时满足的时候才会调用
loadView
方法
RootViewController
*vc = [[
RootViewController
alloc
]
init
];
vc.name = @"heheheheh";
NSLog(@"
创建了
VC"
);
self
.
window
.
rootViewController
= vc;
//
相当于做了下一步的代码
// [self.window addSubview:vc.view];
创建模态视图:由一根视图控制器上的的一个按钮控制
- (
void
)btnClick:(
UIButton
*)btn
{
// 创建弹出来的控制器对象
DetailViewController *detailVC = [[ DetailViewController alloc ] init ];
// 设置弹出的动画效果
/*
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),
*/
detailVC. modalTransitionStyle = UIModalTransitionStyleFlipHorizontal ;
// 弹出模态视图
//iOS6 之前的方法
// [self presentModalViewController:detailVC animated:YES];
// 扩展:获取当前系统版本
NSString *version = [ UIDevice currentDevice ]. systemVersion ;
NSLog ( @"version is %@" , version);
//iOS6.0 之后
[ self presentViewController :detailVC animated : YES completion :^{
// 当弹出动画执行完后执行的代码
NSLog ( @" 已经弹出模态视图 " );
}];
{
// 创建弹出来的控制器对象
DetailViewController *detailVC = [[ DetailViewController alloc ] init ];
// 设置弹出的动画效果
/*
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),
*/
detailVC. modalTransitionStyle = UIModalTransitionStyleFlipHorizontal ;
// 弹出模态视图
//iOS6 之前的方法
// [self presentModalViewController:detailVC animated:YES];
// 扩展:获取当前系统版本
NSString *version = [ UIDevice currentDevice ]. systemVersion ;
NSLog ( @"version is %@" , version);
//iOS6.0 之后
[ self presentViewController :detailVC animated : YES completion :^{
// 当弹出动画执行完后执行的代码
NSLog ( @" 已经弹出模态视图 " );
}];
}
关闭模态视图:
- (
void
)btnClick:(
UIButton
*)btn
{
//iOS6
之前的方法
// [self dismissModalViewControllerAnimated:true];
[ self dismissViewControllerAnimated : YES completion : nil ];
// [self dismissModalViewControllerAnimated:true];
[ self dismissViewControllerAnimated : YES completion : nil ];
}
视图控制器的生命周期:
//
视图将出现在屏幕之前
- ( void )viewWillAppear:( BOOL )animated
{
[ super viewWillAppear :animated];
NSLog ( @" 视图将出现在屏幕之前 " );
}
// 视图已在屏幕上渲染完成
- ( void )viewDidAppear:( BOOL )animated
{
[ super viewDidAppear :animated];
NSLog ( @" 视图已在屏幕上渲染完成 " );
}
// 视图将被从屏幕上移除之前执行
- ( void )viewWillDisappear:( BOOL )animated
{
[ super viewWillDisappear :animated];
NSLog ( @" 视图将被从屏幕上移除之前执行 " );
}
// 视图已经从屏幕上移除
- ( void )viewDidDisappear:( BOOL )animated
{
[ super viewDidDisappear :animated];
NSLog ( @" 视图已经从屏幕上移除 " );
- ( void )viewWillAppear:( BOOL )animated
{
[ super viewWillAppear :animated];
NSLog ( @" 视图将出现在屏幕之前 " );
}
// 视图已在屏幕上渲染完成
- ( void )viewDidAppear:( BOOL )animated
{
[ super viewDidAppear :animated];
NSLog ( @" 视图已在屏幕上渲染完成 " );
}
// 视图将被从屏幕上移除之前执行
- ( void )viewWillDisappear:( BOOL )animated
{
[ super viewWillDisappear :animated];
NSLog ( @" 视图将被从屏幕上移除之前执行 " );
}
// 视图已经从屏幕上移除
- ( void )viewDidDisappear:( BOOL )animated
{
[ super viewDidDisappear :animated];
NSLog ( @" 视图已经从屏幕上移除 " );
}
设置控制器支持的旋转方向:
/*
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};
*/
- ( NSUInteger )supportedInterfaceOrientations
{
//UIInterfaceOrientationMaskAll 支持所有的方向 ,ipad 默认返回 UIInterfaceOrientationMaskAll
//iphone 默认返回 UIInterfaceOrientationMaskAllButUpsideDown
return UIInterfaceOrientationMaskAll ;
}
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};
*/
- ( NSUInteger )supportedInterfaceOrientations
{
//UIInterfaceOrientationMaskAll 支持所有的方向 ,ipad 默认返回 UIInterfaceOrientationMaskAll
//iphone 默认返回 UIInterfaceOrientationMaskAllButUpsideDown
return UIInterfaceOrientationMaskAll ;
}
当控制器旋转时调用的方法:
/*
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};
*/
- (
void
)willRotateToInterfaceOrientation:(
UIInterfaceOrientation
)toInterfaceOrientation duration:(
NSTimeInterval
)duration
{
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeRight :
case UIDeviceOrientationLandscapeRight :
{
UIView *view = [ self . view viewWithTag : 100 ];
view. frame = CGRectMake (( height - 100 ) / 2 , ( width - 100 ) / 2 , 100 , 100 );
break ;
}
case UIInterfaceOrientationPortrait :
case UIInterfaceOrientationPortraitUpsideDown :
{
UIView *view = [ self . view viewWithTag : 100 ];
view. frame = CGRectMake (( width - 100 ) / 2 , ( height - 100 ) / 2 , 100 , 100 );
break ;
}
default :
break ;
}
}
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeRight :
case UIDeviceOrientationLandscapeRight :
{
UIView *view = [ self . view viewWithTag : 100 ];
view. frame = CGRectMake (( height - 100 ) / 2 , ( width - 100 ) / 2 , 100 , 100 );
break ;
}
case UIInterfaceOrientationPortrait :
case UIInterfaceOrientationPortraitUpsideDown :
{
UIView *view = [ self . view viewWithTag : 100 ];
view. frame = CGRectMake (( width - 100 ) / 2 , ( height - 100 ) / 2 , 100 , 100 );
break ;
}
default :
break ;
}
}
单例:
static
Person
*ps =
nil
;
@implementation Person
//+ (Person *)sharePerson
//{
// if (ps == nil) {
// // 加上锁
// @synchronized(self) {
// ps = [[Person alloc] init];
// }
// }
// return ps;
//}
// 单例的另一种创建方法 ----GCD 的一种用法
+ ( Person *)sharePerson
{
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^{
ps = [[ Person alloc ] init ];
});
return ps ;
@implementation Person
//+ (Person *)sharePerson
//{
// if (ps == nil) {
// // 加上锁
// @synchronized(self) {
// ps = [[Person alloc] init];
// }
// }
// return ps;
//}
// 单例的另一种创建方法 ----GCD 的一种用法
+ ( Person *)sharePerson
{
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^{
ps = [[ Person alloc ] init ];
});
return ps ;
}