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

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

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

@property (nonatomic, retain) UIView *v1;
@property (nonatomic, retain) UIView *v2;
@end
=============================
// 类中重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame{
   
self = [super initWithFrame:frame];
   
if (self) {
       
_v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 50)];
       
_v1.backgroundColor = [UIColor yellowColor];
        [
self addSubview:_v1];
        [
_v1 release];
       
       
_v2 = [[UIView alloc] initWithFrame:CGRectMake(70 + 30, 0, 200, 50)];
       
_v2.backgroundColor = [UIColor blueColor];
        [
self addSubview:_v2];
        [
_v2 release];
    }
   
return self;
}
==============================================
// 在AppDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   
// Override point for customization after application launch.
   
self.window.backgroundColor = [UIColor whiteColor];
   
//循环创建3UIView
   
for (int i = 0; i < 3; i++) {
       
//这就是一个自定义的视图
       
//虽然什么都没写
       
MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 300) / 2, 100 + 80*i, 300, 50)];
        myView.
backgroundColor = [UIColor redColor];
        [
self.window addSubview:myView];
        [myView
release];
        myView.
v1.backgroundColor = [UIColor blackColor];
        myView.
v2.backgroundColor = [UIColor blackColor];
    }
    [
self.window makeKeyAndVisible];
   
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 = [super init];
   
if (self) {
       
    }
   
return self;
}

//这里他加载的视图是

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
// Do any additional setup after loading the view.
   
self.view.backgroundColor = [UIColor redColor];
   
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//    view1.backgroundColor = [UIColor greenColor];
    [
self.view addSubview:view1];
   
MyView *myview = [[MyView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    myview.
backgroundColor = [UIColor yellowColor];
    myview.
text.delegate = self;
    [
self.view addSubview:myview];
   
Person *per = [Person new];
    per.
name = @"";
    per.
age = @"nan";
    myview.
label.text = per.name;
    myview.
text.placeholder = per.age;
    [myview.
btn addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [myview
release];
[per
release];
   
   
   
   
}

- (
void)didReceiveMemoryWarning {
     
NSLog(@"内存警告了");
    [
super didReceiveMemoryWarning];
   
//我们需要把不用的视图移除
   
//1.这个视图必须被加载过
   
//2.不能是当前显示窗口
   
// Dispose of any resources that can be recreated.
   
if ([self isViewLoaded] == YES&& !self.view.window) {
       
self.view = nil;
    }
else{
       
NSLog(@"这个视图可能没被加载过 ,或者,是当前显示的窗口");
    }
   
   
}


//屏幕旋转
//1. 允许屏幕旋转
 
//2 设置旋转方向
//3.检测屏幕旋转
- (
BOOL)shouldAutorotate{
   
return YES;
}
//设置全都能转
- (
NSUInteger)supportedInterfaceOrientations{
   
return UIInterfaceOrientationMaskAll;
   
}
//检测屏幕旋转
- (
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 = [[ThreeViewController alloc] init];
   
// 跳转方法
    [
self presentViewController:threeVC animated:NO completion:nil];
   
   
}
===========================================
#import "ThreeViewController.h"

@interface ThreeViewController ()

@end

@implementation ThreeViewController
//  视图加载完成
- (
void)viewDidLoad {
    [
super viewDidLoad];
   
// Do any additional setup after loading the view.
   
self.view.backgroundColor = [UIColor greenColor];
   
   
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.
frame = CGRectMake(100, 100, 100, 100);
    button.
backgroundColor = [UIColor orangeColor];
    [button
addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [
self.view addSubview:button];
   
}

- (
void)didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning];
   
// Dispose of any resources that can be recreated.
}
- (
void)buttonClick:(UIButton *)button{
  
   
//返回上一个视图
    [
self dismissViewControllerAnimated:NO completion: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值