UI 第三课 ⼀、自定义视图 二、视图控制器指定⾃自定义View 三、检测屏幕旋转 四、处理内存警告 五、容器视图控制器
来源:互联网 发布:android 开机启动优化 编辑:程序博客网 时间:2024/05/17 06:44
⼀、自定义视图
二、视图控制器指定⾃自定义View
三、检测屏幕旋转
四、处理内存警告
五、容器视图控制器
//新建一个类继承UIView
#import<UIKit/UIKit.h>
@interfaceMyView : UIView
// 把两个小view声明成属性
// 是为了 方便外面调用
// 把两个小view声明成属性
// 是为了 方便外面调用
@property(nonatomic,retain) UIView *v1;
@property(nonatomic,retain) UIView *v2;
@property(nonatomic,retain) UIView *v2;
@end
=============================
// 类中重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
_v1 = [[UIViewalloc]initWithFrame:CGRectMake(0,0,70,50)];
_v1.backgroundColor= [UIColoryellowColor];
[selfaddSubview:_v1];
[_v1release];
_v2 = [[UIViewalloc]initWithFrame:CGRectMake(70+ 30,0,200,50)];
_v2.backgroundColor= [UIColorblueColor];
[selfaddSubview:_v2];
[_v2release];
}
return self;
self = [superinitWithFrame:frame];
if (self) {
_v1 = [[UIViewalloc]initWithFrame:CGRectMake(0,0,70,50)];
_v1.backgroundColor= [UIColoryellowColor];
[selfaddSubview:_v1];
[_v1release];
_v2 = [[UIViewalloc]initWithFrame:CGRectMake(70+ 30,0,200,50)];
_v2.backgroundColor= [UIColorblueColor];
[selfaddSubview:_v2];
[_v2release];
}
return self;
}
==============================================
// 在AppDelegate.m中
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
self.window= [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
// Override point for customization after application launch.
self.window.backgroundColor= [UIColorwhiteColor];
//循环创建3个UIView
for (inti = 0; i <3; i++) {
//这就是一个自定义的视图
//虽然什么都没写
MyView *myView = [[MyViewalloc]initWithFrame:CGRectMake(([UIScreenmainScreen].bounds.size.width- 300) /2,100 + 80*i,300,50)];
myView.backgroundColor= [UIColorredColor];
[self.windowaddSubview:myView];
[myView release];
myView.v1.backgroundColor= [UIColorblackColor];
myView.v2.backgroundColor= [UIColorblackColor];
}
[self.windowmakeKeyAndVisible];
return YES;
self.window= [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
// Override point for customization after application launch.
self.window.backgroundColor= [UIColorwhiteColor];
//循环创建3个UIView
for (inti = 0; i <3; i++) {
//这就是一个自定义的视图
//虽然什么都没写
MyView *myView = [[MyViewalloc]initWithFrame:CGRectMake(([UIScreenmainScreen].bounds.size.width- 300) /2,100 + 80*i,300,50)];
myView.backgroundColor= [UIColorredColor];
[self.windowaddSubview:myView];
[myView release];
myView.v1.backgroundColor= [UIColorblackColor];
myView.v2.backgroundColor= [UIColorblackColor];
}
[self.windowmakeKeyAndVisible];
return YES;
}
二、视图控制器指定⾃自定义View
//加载视图加载self.view
//用加载视图的方法来实现自定义视图
//- (void)loadView{
// // [super loadView];
// //创建一个view给视图控制器self.view
//
// MyView *myview = [[MyView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
// self.view = myview;//给视图控制器的属性 view赋值
// //[self.view addSubview:myview];//self.view是nil myview加载不上;
//}
//视图的控制器的生命周期
- (void)viewDidAppear:(BOOL)animated{
NSLog(@"显示完成");
}
- (void)viewDidDisappear:(BOOL)animated{
NSLog(@"已经消失");
}
- (void)viewWillAppear:(BOOL)animated{
NSLog(@"视图将要出现");
}
- (void)viewWillDisappear:(BOOL)animated{
NSLog(@"将要消失");
}
//重写初始化方法
- (instancetype)init{
self = [superinit];
if (self) {
}
returnself;
}
//这里他加载的视图是
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorredColor];
UIView *view1 = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];
// view1.backgroundColor = [UIColor greenColor];
[self.viewaddSubview:view1];
MyView *myview = [[MyViewalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
myview.backgroundColor = [UIColoryellowColor];
myview.text.delegate =self;
[self.viewaddSubview:myview];
Person *per = [Personnew];
per.name =@"王";
per.age =@"nan";
myview.label.text = per.name;
myview.text.placeholder = per.age;
[myview.btnaddTarget:selfaction:@selector(buttonClick:)forControlEvents:(UIControlEventTouchUpInside)];
[myview release];
[per release];
}
- (void)didReceiveMemoryWarning {
NSLog(@"内存警告了");
[superdidReceiveMemoryWarning];
//我们需要把不用的视图移除
//1.这个视图必须被加载过
//2.不能是当前显示窗口
// Dispose of any resources that can be recreated.
if ([selfisViewLoaded] ==YES&& !self.view.window) {
self.view =nil;
}else{
NSLog(@"这个视图可能没被加载过 ,或者,是当前显示的窗口");
}
}
//屏幕旋转
//1. 允许屏幕旋转
//2 设置旋转方向
//3.检测屏幕旋转
- (BOOL)shouldAutorotate{
returnYES;
}
//设置全都能转
- (NSUInteger)supportedInterfaceOrientations{
returnUIInterfaceOrientationMaskAll;
}
//检测屏幕旋转
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
NSLog(@"%@",NSStringFromCGSize(size));
}
- (void)buttonClick:(UIButton *)button{
// MyView*myview1 =(MyView *) button.superview;
// //更改bounds
// myview1.bounds = CGRectMake(100,100 , 100, 100);
ThreeViewController *threeVC = [[ThreeViewControlleralloc]init];
// 跳转方法
[selfpresentViewController:threeVCanimated:NOcompletion:nil];
//用加载视图的方法来实现自定义视图
//- (void)loadView{
// // [super loadView];
// //创建一个view给视图控制器self.view
//
// MyView *myview = [[MyView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
// self.view = myview;//给视图控制器的属性 view赋值
// //[self.view addSubview:myview];//self.view是nil myview加载不上;
//}
//视图的控制器的生命周期
- (void)viewDidAppear:(BOOL)animated{
NSLog(@"显示完成");
}
- (void)viewDidDisappear:(BOOL)animated{
NSLog(@"已经消失");
}
- (void)viewWillAppear:(BOOL)animated{
NSLog(@"视图将要出现");
}
- (void)viewWillDisappear:(BOOL)animated{
NSLog(@"将要消失");
}
//重写初始化方法
- (instancetype)init{
self = [superinit];
if (self) {
}
returnself;
}
//这里他加载的视图是
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorredColor];
UIView *view1 = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];
// view1.backgroundColor = [UIColor greenColor];
[self.viewaddSubview:view1];
MyView *myview = [[MyViewalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
myview.backgroundColor = [UIColoryellowColor];
myview.text.delegate =self;
[self.viewaddSubview:myview];
Person *per = [Personnew];
per.name =@"王";
per.age =@"nan";
myview.label.text = per.name;
myview.text.placeholder = per.age;
[myview.btnaddTarget:selfaction:@selector(buttonClick:)forControlEvents:(UIControlEventTouchUpInside)];
[myview release];
[per release];
}
- (void)didReceiveMemoryWarning {
NSLog(@"内存警告了");
[superdidReceiveMemoryWarning];
//我们需要把不用的视图移除
//1.这个视图必须被加载过
//2.不能是当前显示窗口
// Dispose of any resources that can be recreated.
if ([selfisViewLoaded] ==YES&& !self.view.window) {
self.view =nil;
}else{
NSLog(@"这个视图可能没被加载过 ,或者,是当前显示的窗口");
}
}
//屏幕旋转
//1. 允许屏幕旋转
//2 设置旋转方向
//3.检测屏幕旋转
- (BOOL)shouldAutorotate{
returnYES;
}
//设置全都能转
- (NSUInteger)supportedInterfaceOrientations{
returnUIInterfaceOrientationMaskAll;
}
//检测屏幕旋转
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
NSLog(@"%@",NSStringFromCGSize(size));
}
- (void)buttonClick:(UIButton *)button{
// MyView*myview1 =(MyView *) button.superview;
// //更改bounds
// myview1.bounds = CGRectMake(100,100 , 100, 100);
ThreeViewController *threeVC = [[ThreeViewControlleralloc]init];
// 跳转方法
[selfpresentViewController:threeVCanimated:NOcompletion:nil];
}
===========================================
#import"ThreeViewController.h"
@interfaceThreeViewController ()
@end
@implementation ThreeViewController
// 视图加载完成
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorgreenColor];
UIButton *button = [UIButtonbuttonWithType:(UIButtonTypeCustom)];
button.frame =CGRectMake(100,100,100,100);
button.backgroundColor = [UIColororangeColor];
[button addTarget:selfaction:@selector(buttonClick:)forControlEvents:(UIControlEventTouchUpInside)];
[self.viewaddSubview:button];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)buttonClick:(UIButton *)button{
//返回上一个视图
[selfdismissViewControllerAnimated:NOcompletion:nil];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@interfaceThreeViewController ()
@end
@implementation ThreeViewController
// 视图加载完成
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorgreenColor];
UIButton *button = [UIButtonbuttonWithType:(UIButtonTypeCustom)];
button.frame =CGRectMake(100,100,100,100);
button.backgroundColor = [UIColororangeColor];
[button addTarget:selfaction:@selector(buttonClick:)forControlEvents:(UIControlEventTouchUpInside)];
[self.viewaddSubview:button];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)buttonClick:(UIButton *)button{
//返回上一个视图
[selfdismissViewControllerAnimated:NOcompletion:nil];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
0 0
- UI 第三课 ⼀、自定义视图 二、视图控制器指定⾃自定义View 三、检测屏幕旋转 四、处理内存警告 五、容器视图控制器
- 爬爬爬之路:UI(三)自定义视图 MVC浅谈 视图控制器 屏幕旋转事件获取
- 自定义视图控制器容器
- 第三讲:⾃自定义视图、视图控制器
- UI第三节 自定义视图,视图控制器
- UI 第三天 自定义视图 视图控制器
- UI基础-自定义视图、视图控制器
- 进击的KFC:UI(三)自定义视图、视图控制器
- UI基础:视图控制器.屏幕旋转.MVC
- Custom Container View Controller(自定义容器视图控制器)
- 视图控制器和屏幕旋转
- iOS中视图控制器指定自定义UIView
- UI基础知识 -- 容器视图控制器
- UIday02~04:UITextField UIButton UIdelegate 自定义视图 容器视图控制器
- UI开发----自定义视图和视图控制器(Controller)
- 随意细解:UI -- 自定义视图、视图控制器
- UI-第三讲:视图控制器
- UISplitViewController 分割视图控制器,自定义左右视图控制器视图大小
- 在无线互联的这些事连载1
- 《深入浅出数据分析》读后详解
- Opencv学习笔记(五)Harris角点检测
- 第二天上课
- LINUX c++ 遍历网卡列表
- UI 第三课 ⼀、自定义视图 二、视图控制器指定⾃自定义View 三、检测屏幕旋转 四、处理内存警告 五、容器视图控制器
- K - Work
- NVIDIA神卡改个名 1000美刀变5000
- c语言中调用shell脚本返回值问题分析
- 攻击网站的游戏(simple连续的三个数字)
- 局域网内PC通过笔记本共享上网
- CSS实现垂直水平居中
- 多校第三场 1010 hdu 5325 Crazy Bobo(拓扑排序+树形dp)
- Xcode 4-PBXcp error修复-No such file or directory