UI 第三课 ⼀、自定义视图 二、视图控制器指定⾃自定义View 三、检测屏幕旋转 四、处理内存警告 五、容器视图控制器

来源:互联网 发布:android 开机启动优化 编辑:程序博客网 时间:2024/05/17 06:44
⼀、自定义视图
二、视图控制器指定⾃自定义View
三、检测屏幕旋转
四、处理内存警告
五、容器视图控制器

//新建一个类继承UIView
#import<UIKit/UIKit.h>
@interfaceMyView : UIView
// 把两个小view声明成属性
// 是为了 方便外面调用

@property(nonatomic,retain) UIView *v1;
@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;
}
==============================================
// 在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];
   
//循环创建3UIView
   
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.viewnil 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.
}
*/


@end
0 0
原创粉丝点击