CustomViewAndUIViewController

loadView方法内部对self.view进行创建

RootViewController继承于UIViewContrller的子类

自定义视图

    1.设计控件布局

    2.找到底板, 以底板的类为父类, 创建一个子类

    3.针对底板上的控件, 依次写属性

    4.重写父类(UIView)的初始化方法, 在初始化方法内部对底板上的控件进行创建

    5.创建一个自定义视图类的对象, 验证自定义视图(布局, 样式)

 

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];[self.window makeKeyAndVisible];   RootViewController *rootVC = [[RootViewController alloc] init];self.window.rootViewController = rootVC;[rootVC release];  return YES;
}

RootViewController.m

- (void)loadView {
//    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.view = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];NSLog(@"加载视图");
}

视图已经加载完成
- (void)viewDidLoad {NSLog(@"视图已经加载完成");[super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    label.text = @"周一见!";label.tag = 100;[self.view addSubview:label];[label release];self.view.backgroundColor = [UIColor colorWithRed:0.952 green:1.000 blue:0.456 alpha:1.000];    LoginView *loginView = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    loginView.backgroundColor = [UIColor colorWithRed:0.957 green:1.000 blue:0.420 alpha:1.000];
    [self.view addSubview:loginView];
    [loginView release];

}当应用的内存快不够用时, 就会发出警告, 系统会让当前显示的视图控制器执行didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.NSLog(@"收到内存警告");UILabel *label = (UILabel *)[self.view viewWithTag:100];从父视图移除某个子视图
    [label removeFromSuperview];
}

#import "HomeViewController.h"
#define kSize 100
@interface HomeViewController () {UILabel *label2;UILabel *label3;UILabel *label4;
}@end
@implementation HomeViewController
- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor = [UIColor colorWithRed:0.998 green:0.704 blue:1.000 alpha:1.000];UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kSize, kSize)];label1.backgroundColor = [UIColor yellowColor];label1.text = @"1";label1.font = [UIFont systemFontOfSize:80];label1.textAlignment = NSTextAlignmentCenter;[self.view addSubview:label1];[label1 release];   label2 = [[UILabel alloc] initWithFrame:CGRectMake(375 - kSize, 0, kSize, kSize)];label2.backgroundColor = [UIColor yellowColor];label2.text = @"2";label2.font = [UIFont systemFontOfSize:80];label2.textAlignment = NSTextAlignmentCenter;[self.view addSubview:label2];[label2 release];label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 667 - kSize, kSize, kSize)];label3.backgroundColor = [UIColor yellowColor];label3.text = @"3";label3.font = [UIFont systemFontOfSize:80];label3.textAlignment = NSTextAlignmentCenter;[self.view addSubview:label3];[label3 release];label4 = [[UILabel alloc] initWithFrame:CGRectMake(375 - kSize, 667 - kSize, kSize, kSize)];label4.backgroundColor = [UIColor yellowColor];label4.backgroundColor = [UIColor yellowColor];label4.text = @"4";label4.font = [UIFont systemFontOfSize:80];label4.textAlignment = NSTextAlignmentCenter;[self.view addSubview:label4];[label4 release];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}UIViewController控制旋转的方法
是否支持旋转, 默认YES
- (BOOL)shouldAutorotate {return YES;
}- (NSUInteger)supportedInterfaceOrientations {        UIInterfaceOrientationMaskPortrait: 肖像模式UIInterfaceOrientationMaskLandscapeRight: 风景模式右UIInterfaceOrientationMaskPortraitUpsideDown: 肖像模式倒置UIInterfaceOrientationMaskLandscape: 风景模式(左, 右)UIInterfaceOrientationMaskAll: 四个方向UIInterfaceOrientationMaskAllButUpsideDown: 除了肖像模式倒置的所有方向iPhone项目 默认:  UIInterfaceOrientationMaskAllButUpsideDowniPad项目 默认: UIInterfaceOrientationMaskAllreturn UIInterfaceOrientationMaskAll;
}视图将要旋转, 执行这个方法
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {size, 当前视图的大小NSLog(@"%@", NSStringFromCGSize(size));if (size.width > size.height) {NSLog(@"横屏");view的frame不允许单独修改, 必须整体修改//1
        label2.frame = CGRectMake(667 - kSize, 0, kSize, kSize);//2
        CGRect rect = label2.frame;
        rect.origin.x = size.width - kSize;
        label2.frame = rect;} else {NSLog(@"竖屏");
       label2.frame = CGRectMake(size.width - kSize, 0, kSize, kSize);
       }CGRect rect = label2.frame;rect.origin.x = size.width - kSize;label2.frame = rect;rect = label3.frame;rect.origin.y = size.height - kSize;label3.frame = rect;rect = label4.frame;rect.origin.y = size.height - kSize;rect.origin.x = size.width - kSize;label4.frame = rect;
}

封装Label - TextField界面

LTView.h
#import <UIKit/UIKit.h>
@interface LTView : UIView
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UITextField *textField;
@end

LTView.m
#import "LTView.h"
@implementation LTView
- (void)dealloc {[_label release];[_textField release];[super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {//label_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, frame.size.height)];
//        _label.backgroundColor = [UIColor colorWithRed:0.426 green:1.000 blue:0.956 alpha:1.000];_label.textAlignment = NSTextAlignmentCenter;[self addSubview:_label];[_label release];        //textField_textField = [[UITextField alloc] initWithFrame:CGRectMake(_label.frame.size.width, 0, frame.size.width - _label.frame.size.width, frame.size.height)];//frame.size.width / 3 * 2
//        _textField.backgroundColor = [UIColor whiteColor];
        [_textField setBorderStyle:(UITextBorderStyleRoundedRect)];[self addSubview:_textField];[_textField release];     }return self;
}
@end

封装Login界面

LoginView.h
#import <UIKit/UIKit.h>
@class LTView;
@interface LoginView : UIView
@property (nonatomic, retain) LTView *userNameView, *passwordView;
@property (nonatomic, retain) UIButton *button;
@end

LoginView.m
#import "LoginView.h"
#import "LTView.h"
@implementation LoginView
- (void)dealloc {[_userNameView release];[_passwordView release];[_button release];[super dealloc];
}- (instancetype)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {//用户名self.userNameView = [[LTView alloc] initWithFrame:CGRectMake(50, 100, 275, 40)];_userNameView.label.text = @"用 户 名";_userNameView.textField.placeholder = @" 请输入用户名";[self addSubview:_userNameView];[_userNameView release];//密码self.passwordView = [[LTView alloc] initWithFrame:CGRectMake(50, 160, 275, 40)];_passwordView.label.text = @"密      码";_passwordView.textField.placeholder = @" 请输入密码";_passwordView.textField.secureTextEntry = YES;[self addSubview:_passwordView];[_passwordView release];//登录按钮self.button = [UIButton buttonWithType:UIButtonTypeSystem];_button.frame = CGRectMake(100, 220, 375 - 200, 40);
//        _button.backgroundColor = [UIColor greenColor];
//        _button.showsTouchWhenHighlighted = YES;[_button setTitle:@"登录" forState:UIControlStateNormal];_button.titleLabel.font = [UIFont systemFontOfSize:20];[self addSubview:_button];}return self;
}
@end

 MVC: 是一种设计框架

    M: model, 数据模型, 用于定义数据结构, 存储数据

    V: view, 视图, 用于展示内容

    C: controller, 控制器, 作为modelview的协调者

    1.MVC,面向对象程序中出现的对象进行分类, 规定各自的作用

    2.modelview之间不能通信

    3.controller可以访问modelview, modelview不能访问controller

    4.view可以间接访问controller, 主要的通信手段

     a.target-action(目标动作机制)

     b.delegate(代理模式)

     c.dataSource(也是代理模式, 只不过换了一个名字, 主要用于数据的传递)

    5.moodel可以间接访问controller, 主要的通信手段

     a.KVO(键值观察)

     b.notification(通知)

    UIViewController, 视图控制器, 属于控制类, viewmodel的协调者, MVC框架的核心, 继承于UIResponder, 管理iOS应用中的view

    appDelegate中的视图创建的的代码, 转移到UIViewController

    RootViewController *rootVC = [[RootViewController alloc] init];

    指定window的根视图控制器

    self.window.rootViewController = rootVC;

    [rootVC release];

    : 视图控制器会自带一个UIView, 当指定window的根视图控制器后, 会出现window的上方, 并且和window的大小相同

 

转载于:https://www.cnblogs.com/OrangesChen/p/4895863.html

自定义视图 视图控制器(UIViewController)相关推荐

  1. UI 自定义视图 ,视图管理器

    一>自定义label - textField 视图 自定义视图:系统标准UI之外,自己组合而出的新的视图 iOS 提供了很多UI组件 ,借助它们,我们可以做各种程序 尽管如此,实际开发中,我们还 ...

  2. ASP.NET MVC 自定义Razor视图WorkContext

    概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...

  3. swift 自定义滑动视图_在Swift中创建一个向上滑动菜单视图(以编程方式)

    swift 自定义滑动视图 This is a quick tutorial on how to create a slide-up menu view in iOS 这是有关如何在iOS中创建向上滑 ...

  4. ASP.NET MVC教程:理解模型、视图和控制器(1)

    本文对ASP.NET MVC的模型.视图和控制器做一次高级概述,换句话说,就是解释ASP.NET MVC中的"M""V""C".阅读完本文后 ...

  5. Salesforce视图与控制器之间的交互

    刚接触Salesforce,过程的确是比较艰难了,中文资料几乎没有,看英文资料学的效率却不高,不过看了一段时间的英文资料发现自己英语水平挺高不少啊,现在看都不用工具翻译,早知道就再次尝试报个6级,看下 ...

  6. 了解模型、视图和控制器

    了解模型.视图和控制器 本教程对 ASP.NET MVC 的模型.视图和控制器提供了高层面的概述.换句话说,它解释了 ASP.NET MVC 中的 'M'.'V'和'C'. 阅读本教程后,您将会了解 ...

  7. .ne中的控制器循环出来的数据如何显示在视图上_【asp.net core 系列】3 视图以及视图与控制器...

    0.前言 在之前的几篇中,我们大概介绍了如何创建一个http://asp.net core mvc项目以及http请求如何被路由转交给对应的执行单元.这一篇我们将介绍一下控制器与视图直接的关系. 1. ...

  8. [翻译:ASP.NET MVC 教程]理解模型、视图和控制器

    本篇教程为你提供了ASP.NET MVC的模型.视图和控制器的高级概述.换句话说,即本文向你解释了在ASP.NET MVC中"M"."V"和"C&qu ...

  9. 从视图到控制器的传值方法(表单)

    从视图到控制器的传值方法(表单) Views中: 将数据提交到某个控制器的方法中,在该方法中去做处理 <form action="/控制器/get"method=" ...

最新文章

  1. Window对象中的函数confirm方法的简单介绍
  2. HTML 按钮(button)的 disable 属性和 disable property
  3. 几种多数据库表update的方式测试
  4. int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null.....
  5. python编程书籍1020python编程书籍_从零单排之玩转Python安全编程(II)
  6. python的pygame库使用方法_python基础教程使用Python第三方库pygame写个贪吃蛇小游戏...
  7. 【tensorflow】常量张量的初始化
  8. 【SpringCloud】服务降级 Hystrix 断路器
  9. Sentinel一键下载安装运行_分布式系统集群限流_线程数隔离_削峰填谷_流量控制_速率控制_服务熔断_服务降级---微服务升级_SpringCloud Alibaba工作笔记0030
  10. java 获取pdf总页数_如何获取PDF文件的总页数 - iTextSharp,c#
  11. ubuntu 时区 修改时间 保存 重启 变化等
  12. c# mysql varbinary_SQL Server 中,实现 varbinary 与 varchar 类型之间的数据转换
  13. 大卫科波菲尔优秀读后感范文4000字
  14. office2016 excel复制粘贴就卡死
  15. WeiPHP5.0,公众号与小程序结合的最佳开发框架
  16. 正则表达式匹配字符串(scala)
  17. VUE+WebPack游戏设计:欲望都市,构建类RPG游戏的开发
  18. 机器学习中级教程 7.数据泄漏
  19. 使用elastic job管理调度定时任务
  20. Zemax中的优化算法

热门文章

  1. Scala流程控制语句值顺序控制
  2. Flink的主要特点及与Spark的对比
  3. Java访问修饰符public,private,protected,以及不写(默认)时的区别?
  4. linux下的几种进程间通信方式的特点
  5. 字节、半字、字对齐方式
  6. python 异常函数调用栈_Python捕获异常堆栈信息的几种方法
  7. HTML怎么把文字分栏_JS将HTML生成PDF并下载
  8. matlab hsv提取s_Matlab进阶教程 | 基于不规则已知点插值
  9. L、TEXT()、_TEXT()和_T()的区别
  10. mysql linux 安装部署,linux之MySQL安装部署(示例代码)