UI-自定义视图、视图控制器
自定义视图
- 自定义视图:系统标准UI之外,自己组合而出的新的视图。
- iOS提供了很多UI组件,借助它们,我们可以做各种程序。
创建步骤
- 创建一个UIView子类
- 在类的初始化方法中添加子视图
- 类的.h文件提供一些接口(方法),便于外界操作子视图
视图控制器
- UIViewController:视图控制器;
- 控制视图显示,响应事件;
- 分担AppDelegate的工作;
- 实现模块独立,提高复用性。
视图控制器功能
- 控制试图大小变换、布局视图、响应事件
- 检测以及处理内存警告
- 检测以及处理屏幕旋转
- 检测视图的切换
MVC概述
- UIViewController是MVC设计模式的核心
- MVC是一个框架级的设计模式
- M是Model,主要用于建立数据模型(即数据的结构)
- V是View,我们能看到的所有控件都是view,view主要的功能是阐释数据
- C是控制器,主要是控制M和V的通
主要练习实例
// AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
// AppDelegate.m
#import "AppDelegate.h"
//引入自己创建的视图控制器
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
//创建视图控制器
ViewController *vc = [[ViewController alloc]init];
//用我们创建的视图控制器代替自身的视图控制器
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
//引入自己创建的视图控制器
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
//创建视图控制器
ViewController *vc = [[ViewController alloc]init];
//用我们创建的视图控制器代替自身的视图控制器
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
// YDview.h
#import <UIKit/UIKit.h>
@interface YDview : UIView
@property(nonatomic,retain)UILabel *lable;
@property(nonatomic,retain)UITextField *textfield;
@property(nonatomic,retain)UIButton *button;
@end
// YDview.m
#import "YDview.h"
@implementation YDview
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
[self addAllable];
}
return self;
}
-(void)addAllable{
_lable = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 70, 30)];
_lable.backgroundColor = [UIColor whiteColor];
_lable.text = @"用 户 名:";
[self addSubview:_lable];
_textfield = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_lable.frame)+20, CGRectGetMinX(_lable.frame), CGRectGetWidth(_lable.frame)+60, CGRectGetHeight(_lable.frame))];
_textfield.backgroundColor = [UIColor whiteColor];
[self addSubview:_textfield];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(CGRectGetMinX(_lable.frame), CGRectGetMaxY(_lable.frame)+20, CGRectGetWidth(_lable.frame)+20, CGRectGetHeight(_lable.frame));
[_button setTitle:@"登陆" forState:UIControlStateNormal];
[_button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
_button.backgroundColor = [UIColor greenColor];
[self addSubview:_button]; }/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code}*/
@end
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
#import "YDview.h"
@interface ViewController ()<UITextFieldDelegate>
//声明一个
@property(nonatomic,retain)YDview *yview;
@end
@implementation ViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_yview = [[YDview alloc]initWithFrame:[UIScreen mainScreen].bounds];
// _yview.backgroundColor = [UIColor yellowColor];
}
return self;
}
-(void)loadView{
self.view = _yview;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
_yview.textfield.delegate = self;
[_yview.button addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
}
-(void)abc{
NSLog(@"登陆成功呢了");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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.
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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