【UI初级--连载五】---------UIViewController(视图控制器)
来源:互联网 发布:h5棋牌源码定制 编辑:程序博客网 时间:2024/06/05 17:45
内容简介:
1、视图控制器根视图的加载
2、视图控制器生命周期
3、模态视图
4、视图控制器旋转方向
5、单例
一、视图控制器根视图的加载
//复写的loadView方法
//调用loadView方法一定要给自己一个View
/*
调用loadView方法需要满足的条件:
1、view属性的get方法被调用的时候
2、view为空的时候
//调用loadView方法一定要给自己一个View
/*
调用loadView方法需要满足的条件:
1、view属性的get方法被调用的时候
2、view为空的时候
只有当以上两个条件同时满足的时候才会调用loadView方法
//注意:如果需要使用storyboard或者xib则一定不要复写loadView方法
*/
【例子】:
RootViewController.h文件中:有属性:name
AppDelegate.m文件中:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
//*******创建window窗口*********************
//拿到屏幕的大小
CGRect rect = [UIScreenmainScreen].bounds;
//创建一个window
self.window= [[UIWindowalloc]initWithFrame:rect];
//设置窗口颜色
self.window.backgroundColor= [UIColorgrayColor];
//把当前的window作为程序的主window显示出来
[self.windowmakeKeyAndVisible];
NSLog(@"将要创建rootVC");
//创建根视图控制器
RootViewController *rootVC = [[RootViewControlleralloc]init];
// Override point for customization after application launch.
//*******创建window窗口*********************
//拿到屏幕的大小
CGRect rect = [UIScreenmainScreen].bounds;
//创建一个window
self.window= [[UIWindowalloc]initWithFrame:rect];
//设置窗口颜色
self.window.backgroundColor= [UIColorgrayColor];
//把当前的window作为程序的主window显示出来
[self.windowmakeKeyAndVisible];
NSLog(@"将要创建rootVC");
//创建根视图控制器
RootViewController *rootVC = [[RootViewControlleralloc]init];
//init以后,它会自动调用视图控制器中相关的init方法
//如果自己添加了MyXib.xib文件,则用以下的初始化方法,并且一定不能重写loadView方法
//RootViewController *rootVC = [[RootViewController alloc] initWithNibName:@"MyXib" bundle:nil];
rootVC.name=@"呵呵呵呵呵";
NSLog(@"创建了rootVC");
[self.windowsetRootViewController:rootVC];
// [self.window addSubview:rootVC.view];//相当于上句
NSLog(@"rootVC已做完跟试图控制器");
//执行顺序:
/*
将要创建rootVC
创建了rootVC
这是loadView方法
这是viewDidLoad方法
rootVC已做完跟试图控制器
*/
//加上RootViewController.m中重写了(初始化方法)后:
/*
将要创建rootVC
//这是初始化方法
这是loadView方法
这是viewDidLoad方法
//初始化方法结束
//创建了rootVC
rootVC已做完跟试图控制器
*/
return YES;
}
NSLog(@"创建了rootVC");
[self.windowsetRootViewController:rootVC];
// [self.window addSubview:rootVC.view];//相当于上句
NSLog(@"rootVC已做完跟试图控制器");
//执行顺序:
/*
将要创建rootVC
创建了rootVC
这是loadView方法
这是viewDidLoad方法
rootVC已做完跟试图控制器
*/
//加上RootViewController.m中重写了(初始化方法)后:
/*
将要创建rootVC
//这是初始化方法
这是loadView方法
这是viewDidLoad方法
//初始化方法结束
//创建了rootVC
rootVC已做完跟试图控制器
*/
return YES;
}
RootViewController.m文件中:
@implementationRootViewController
//复写的初始化方法
- (instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
NSLog(@"这是初始化方法");
self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if (self) {
//...
self.view.backgroundColor= [UIColorredColor];
//这里调用了View的get方法,完成后在调用(view.backgroundColor)set方法。
}
NSLog(@"初始化方法结束");
return self;
}
//复写的loadView方法
//调用loadView方法一定要给自己一个View
/*
调用loadView方法需要满足的条件:
1、view属性的get方法被调用的时候
2、view为空的时候
//复写的初始化方法
- (instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
NSLog(@"这是初始化方法");
self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if (self) {
//...
self.view.backgroundColor= [UIColorredColor];
//这里调用了View的get方法,完成后在调用(view.backgroundColor)set方法。
}
NSLog(@"初始化方法结束");
return self;
}
//复写的loadView方法
//调用loadView方法一定要给自己一个View
/*
调用loadView方法需要满足的条件:
1、view属性的get方法被调用的时候
2、view为空的时候
只有当以上两个条件同时满足的时候才会调用loadView方法
//注意:如果需要使用storyboard或者xib则一定不要复写loadView方法
*/
- (void)loadView{
NSLog(@"这是loadView方法");
UIView *view = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
view.backgroundColor= [UIColoryellowColor];
self.view= view;
}
//视图已经加载了,会调用这个方法
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
// self.view.backgroundColor = [UIColor greenColor];
NSLog(@"这是viewDidLoad方法");
UILabel *lable = [[UILabelalloc]initWithFrame:CGRectMake(100,100,100,50)];
lable.backgroundColor= [UIColorredColor];
lable.text=self.name;
[self.viewaddSubview:lable];
}
- (void)loadView{
NSLog(@"这是loadView方法");
UIView *view = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
view.backgroundColor= [UIColoryellowColor];
self.view= view;
}
//视图已经加载了,会调用这个方法
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
// self.view.backgroundColor = [UIColor greenColor];
NSLog(@"这是viewDidLoad方法");
UILabel *lable = [[UILabelalloc]initWithFrame:CGRectMake(100,100,100,50)];
lable.backgroundColor= [UIColorredColor];
lable.text=self.name;
[self.viewaddSubview:lable];
}
二、视图控制器生命周期
注意:使用时最好调用一下他们父类的方法
例如:[super viewWillAppear:animated];
//视图将出现在屏幕之前
- (void)viewWillAppear:(BOOL)animated
//视图已在屏幕上渲染完成
- (void)viewDidAppear:(BOOL)animated
//视图将被从屏幕上移除之前执行
- (void)viewWillDisappear:(BOOL)animated
//视图已经从屏幕上移除
- (void)viewDidDisappear:(BOOL)animated
- (void)viewDidDisappear:(BOOL)animated
三、模态视图
//创建弹出来的控制器对象
DetailViewController *detailVC = [[DetailViewControlleralloc]init];
//扩展:获取当前系统的版本
NSString *version = [UIDevicecurrentDevice].systemVersion;
NSLog(@"veersion is %@",version);
//设置弹出的动画效果
/*
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),
*/
detailVC.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
//弹出模态视图
//ios6.0以前的版本
// [self presentModalViewController:detailVC animated:YES];
//ios6.0以后的版本
[selfpresentViewController:detailVCanimated:YEScompletion:^{
//当执行完弹出动画后执行的代码
NSLog(@"已经弹出模态视图");
}];
DetailViewController *detailVC = [[DetailViewControlleralloc]init];
//扩展:获取当前系统的版本
NSString *version = [UIDevicecurrentDevice].systemVersion;
NSLog(@"veersion is %@",version);
//设置弹出的动画效果
/*
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),
*/
detailVC.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
//弹出模态视图
//ios6.0以前的版本
// [self presentModalViewController:detailVC animated:YES];
//ios6.0以后的版本
[selfpresentViewController:detailVCanimated:YEScompletion:^{
//当执行完弹出动画后执行的代码
NSLog(@"已经弹出模态视图");
}];
- (void)btnClick:(UIButton*)btn{
//关闭模态视图
//iOS6之前的方法
[selfdismissModalViewControllerAnimated:true];
//iOS6之后的方法
[selfdismissViewControllerAnimated:truecompletion:nil];
//iOS6之前的方法
[selfdismissModalViewControllerAnimated:true];
//iOS6之后的方法
[selfdismissViewControllerAnimated:truecompletion:nil];
}
四、视图控制器旋转方向
@interfaceRootViewController()
{
CGFloat width;
CGFloat height;
}
@end
@implementationRootViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
width = [UIScreenmainScreen].bounds.size.width;
height = [UIScreenmainScreen].bounds.size.height;
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake((width-100)/2, (height-100)/2,100,100)];
view.backgroundColor= [UIColorredColor];
view.tag=10;
[self.viewaddSubview:view];
}
//控制器支持的旋转方向
/*
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
//支持向上(Home键在下)
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
//支持向左(Home键在右)
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
//支持向右(Home键在左)
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
//支持向下(Home键在上)
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
支持向左、向右
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
支持四个方向
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
支持向上、向左、向右
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};
*/
//设置控制器支持的旋转方向
- (NSUInteger)supportedInterfaceOrientations{
//支持所有方向(iPad默认)
return UIInterfaceOrientationMaskAll;
//支持向上、向左、向右(iPhone默认)
return UIInterfaceOrientationMaskAllButUpsideDown;
}
/*
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
//屏幕方向未知
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
//屏幕正向(Home键在下)
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
//屏幕倒置(Home键在上)
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};
*/
//当控制器旋转时调用的方法
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
UIView *view = [self.viewviewWithTag:10];
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeRight:
case UIInterfaceOrientationLandscapeLeft:
{
view.frame=CGRectMake((height-100)/2, (width-100)/2,100,100);
break;
}
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
{
view.frame=CGRectMake((width-100)/2, (height-100)/2,100,100);
break;
}
default:
break;
}
}
{
CGFloat width;
CGFloat height;
}
@end
@implementationRootViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
width = [UIScreenmainScreen].bounds.size.width;
height = [UIScreenmainScreen].bounds.size.height;
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake((width-100)/2, (height-100)/2,100,100)];
view.backgroundColor= [UIColorredColor];
view.tag=10;
[self.viewaddSubview:view];
}
//控制器支持的旋转方向
/*
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
//支持向上(Home键在下)
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
//支持向左(Home键在右)
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
//支持向右(Home键在左)
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
//支持向下(Home键在上)
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
支持向左、向右
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
支持四个方向
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
支持向上、向左、向右
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};
*/
//设置控制器支持的旋转方向
- (NSUInteger)supportedInterfaceOrientations{
//支持所有方向(iPad默认)
return UIInterfaceOrientationMaskAll;
//支持向上、向左、向右(iPhone默认)
return UIInterfaceOrientationMaskAllButUpsideDown;
}
/*
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
//屏幕方向未知
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
//屏幕正向(Home键在下)
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
//屏幕倒置(Home键在上)
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};
*/
//当控制器旋转时调用的方法
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
UIView *view = [self.viewviewWithTag:10];
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeRight:
case UIInterfaceOrientationLandscapeLeft:
{
view.frame=CGRectMake((height-100)/2, (width-100)/2,100,100);
break;
}
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
{
view.frame=CGRectMake((width-100)/2, (height-100)/2,100,100);
break;
}
default:
break;
}
}
五、单例
#import"Person.h"
staticPerson*ps =nil;
@implementationPerson
//单例的第一种创建方法
+ (Person*)sharePerson{
if (ps==nil) {
//加上锁
@synchronized(self){
ps = [[Personalloc]init];
}
}
return ps;
}
//单例的另一种创建方法----GCD的一种用法
+ (Person*)sharePerson{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ps = [[Personalloc]init];
});
return ps;
}
staticPerson*ps =nil;
@implementationPerson
//单例的第一种创建方法
+ (Person*)sharePerson{
if (ps==nil) {
//加上锁
@synchronized(self){
ps = [[Personalloc]init];
}
}
return ps;
}
//单例的另一种创建方法----GCD的一种用法
+ (Person*)sharePerson{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ps = [[Personalloc]init];
});
return ps;
}
@end
0 0
- 【UI初级--连载五】---------UIViewController(视图控制器)
- iOS UI初级-视图控制器UIViewController
- UI 视图控制器UIViewController
- UI -视图控制器UIViewController
- 【UI初级--连载六】---------导航控制器(UINavigationController)
- UI笔记_视图控制器UIViewController
- UI初级连载八------------标签控制器
- 【UI初级------连载七】标签控制器
- UI初级第四课 视图控制器——iOS学习连载18
- 【UI初级--连载八】-------------表视图(UITableView)
- UI初级连载12-------------滑动视图
- UI初级第六课 Tab 控制器(标签控制器)——iOS学习连载21
- UI 多个视图控制器(UIViewController)间的 协议传值
- 视图控制器 ( UIViewController )
- UIViewController视图控制器
- UIViewController 视图控制器
- 视图控制器UIViewcontroller
- UIViewController/视图控制器
- 从数据库取出被 特殊符号被转义实体后的数据
- 重学C++Primer笔记3---一开始就要有好的习惯
- Android Studio(十一):代码混淆及打包apk
- 亮相SIBT!物联智能家居这次有点不同
- Socket编程(c语言示例)
- 【UI初级--连载五】---------UIViewController(视图控制器)
- 九九乘法表
- java里的byte
- 解决Command /usr/bin/codesign failed with exit code 1.rtfd
- linux下C语言socket网络编程简例
- 红米NOTE拆机视屏 红米NOTE换总成
- Regional Changchun Online--Elven Postman(裸排序二叉树)
- Android之ADB常用命令
- Java管理对象神奇之Object类的equals和hashcode