//指定根视图控制器
LoginViewController *loginVC = [[LoginViewController alloc] init];
self.window.rootViewController = loginVC;
[loginVC release];
//自定义视图第一步:明确该视图内部有什么控件,并且将所有控件声明成属性
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UITextField *textField;
//自定义初始化方法
- (instancetype)initWithFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)font borderStyle:(UITextBorderStyle)borderStyle placeholder:(NSString *)placeholder secureTextEntry:(BOOL)secureTextEntry keyboardType:(UIKeyboardType)keyboardType;
@end
#import "LTView.h"
@implementation LTView
- (void)dealloc {
[_label release];
[_textField release];
[super dealloc];
}
//重写LTView继承自UIView的布局方法,来创建子视图,并且添加子视图
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//用参数中的frame(LTView视图整体的frame)来表示内部控件的frame
//LTView的宽和高
CGFloat totalWidth = frame.size.width;
CGFloat totalHeight = frame.size.height;
//label的宽和高
CGFloat labelWidth = (totalWidth - 15) / 3;
CGFloat labelHeight = totalHeight - 4;
//textField的宽和高
CGFloat textFieldWidth = 2 * labelWidth;
CGFloat textFieldHeight = labelHeight;
_label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, labelWidth, labelHeight)];
// _label.backgroundColor = [UIColor redColor];
[self addSubview:_label];
_textField = [[UITextField alloc] initWithFrame:CGRectMake(10 + labelWidth, 2, textFieldWidth, textFieldHeight)];
// _textField.backgroundColor = [UIColor blueColor];
[self addSubview:_textField];
}
return self;
}
//自定义初始化方法
- (instancetype)initWithFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)font borderStyle:(UITextBorderStyle)borderStyle placeholder:(NSString *)placeholder secureTextEntry:(BOOL)secureTextEntry keyboardType:(UIKeyboardType)keyboardType {
self = [self initWithFrame:frame];
if (self) {
self.label.text = text;
self.label.textColor = textColor;
self.label.font = font;
self.textField.borderStyle = borderStyle;
self.textField.placeholder = placeholder;
self.textField.secureTextEntry = secureTextEntry;
self.textField.keyboardType = keyboardType;
}
return self;
}
#import <UIKit/UIKit.h>
#import "LTView.h"
@interface LoginView : UIView
@property (nonatomic, retain) LTView *userNameLTV;//用户名
@property (nonatomic, retain) LTView *passwordLTV;//密码
@property (nonatomic, retain) UIButton *loginButton;//登录
@property (nonatomic, retain) UIButton *registButton;//注册
@property (nonatomic, retain) UIButton *getbackButton;//找回密码
@end
#import "LoginView.h"
@interface LoginView ()
//创建UIButton的方法
- (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title;
@end
@implementation LoginView
- (void)dealloc {
[_userNameLTV release];
[_passwordLTV release];
[super dealloc];
}
- (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = frame;
[button setTitle:title forState:UIControlStateNormal];
return button;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//获取frame的宽和高
CGFloat totalWidth = frame.size.width;
// CGFloat totalHeight = frame.size.height;
//获取button的宽度
CGFloat buttonWidth = (totalWidth - 120) / 3;
//创建控件
_userNameLTV = [[LTView alloc] initWithFrame:CGRectMake(30, 80, totalWidth - 60, 40) text:@"用户名:" textColor:[UIColor blackColor] font:[UIFont boldSystemFontOfSize:18] borderStyle:UITextBorderStyleRoundedRect placeholder:@"请输入用户名" secureTextEntry:NO keyboardType:UIKeyboardTypeDefault];
[self addSubview:_userNameLTV];
_passwordLTV = [[LTView alloc] initWithFrame:CGRectMake(30, 130, totalWidth - 60, 40) text:@"密 码:" textColor:[UIColor blackColor] font:[UIFont boldSystemFontOfSize:18] borderStyle:UITextBorderStyleRoundedRect placeholder:@"请输入密码" secureTextEntry:YES keyboardType:UIKeyboardTypeNumberPad];
[self addSubview:_passwordLTV];
_loginButton = [self createButtonWithFrame:CGRectMake(30, 200, buttonWidth, 40) title:@"登录"];
[self addSubview:_loginButton];
_registButton = [self createButtonWithFrame:CGRectMake(buttonWidth + 60, 200, buttonWidth, 40) title:@"注册"];
[self addSubview:_registButton];
_getbackButton = [self createButtonWithFrame:CGRectMake(2 * buttonWidth + 90, 200, buttonWidth, 40) title:@"找回密码"];
[self addSubview:_getbackButton];
/*
_loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
_loginButton.frame = CGRectMake(30, 200, (totalWidth - 120) / 3, 40);
[_loginButton setTitle:@"登录" forState:UIControlStateNormal];
[self addSubview:_loginButton];*/
}
return self;
}
//登陆视图控制器
#import <UIKit/UIKit.h>
#import "LoginView.h"
@interface LoginViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic, retain) LoginView *loginView;
#import "LoginViewController.h"
#import "RegistViewController.h"
@interface LoginViewController ()
@end
@implementation LoginViewController
- (void)dealloc {
[_loginView release];
[super dealloc];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
//加载根视图的方法,我们通常在这个方法中,指定根视图为我们想要的某个视图
//并且在一个视图控制器的生命周期,此方法只会走一次。
//在加载方法中,不能使用self.view这个getter方法获取根视图,因为此时根视图正在加载,并没有真实存在。
- (void)loadView {
//创建loginView
_loginView = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//将loginView指定为self.view
self.view = _loginView;
// [self.view addSubview:_loginView];
//为输入框设置代理
_loginView.userNameLTV.textField.delegate = self;
// _loginView.passwordLTV.textField.delegate = self;
//为button添加点击事件
[_loginView.loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
[_loginView.registButton addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
[_loginView.getbackButton addTarget:self action:@selector(getback) forControlEvents:UIControlEventTouchUpInside];
}
- (void)login {
NSLog(@"登录成功");
[_loginView.userNameLTV.textField resignFirstResponder];
[_loginView.passwordLTV.textField resignFirstResponder];
}
- (void)regist {
NSLog(@"注册");
[_loginView.userNameLTV.textField resignFirstResponder];
[_loginView.passwordLTV.textField resignFirstResponder];
//创建注册页面的视图控制器对象
RegistViewController *registVC = [[RegistViewController alloc] init];
//模态推出新的视图控制器
[self presentViewController:registVC animated:YES completion:nil];
[registVC release];
}
- (void)getback {
NSLog(@"找回密码");
[_loginView.userNameLTV.textField resignFirstResponder];
[_loginView.passwordLTV.textField resignFirstResponder];
}
//视图控制器自带的根视图加载完毕的方法,默认根视图是空白视图,并且背景色是透明色。
//如果想要显示内容,只需要在此方法内部创建视图,并且添加到根视图上面。
//注册视图控制器
#import "RegistViewController.h"
@interface RegistViewController ()
@end
@implementation RegistViewController
- (void)back {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 200, 40);
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view.
}
从这里可以看到自定义视图的全部过程,主要是按照步骤,先声明属性,然后对属性进行实现,
原文:http://www.cnblogs.com/wwww543623/p/5171226.html