博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
封装代理
阅读量:4556 次
发布时间:2019-06-08

本文共 3288 字,大约阅读时间需要 10 分钟。

LTView.h

@interface LTView : UIView {    UILabel *_lable;    UITextField *_textField;}#pragma mark - 自己定义初始化方法- (instancetype)initWithFrame:(CGRect)frame                         text:(NSString *)text                  placeHolder:(NSString *)placeHolder                     isSecure:(BOOL)isSecuer;#pragma mark - textField 的代理- (void)setTextFieldDelegate:(id
)delegate;#pragma mark - 获取 textField- (UITextField *)getTextField;

LTView.m

@implementation LTView#pragma mark - 自己定义初始化方法- (instancetype)initWithFrame:(CGRect)frame                         text:(NSString *)text                  placeHolder:(NSString *)placeHolder                     isSecure:(BOOL)isSecuer{    self = [super initWithFrame:frame];    if (self) {        [self createLabel:text];        [self createTextField:placeHolder isSecure:isSecuer];    }    return self;}- (void)createLabel:(NSString *)text{    _lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 4, self.frame.size.height)];    _lable.text = text;    [self addSubview:_lable];    [_lable release];}- (void)createTextField:(NSString *)placeHolder               isSecure:(BOOL)secure{    _textField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_lable.frame), 0, self.frame.size.width / 4 * 3, self.frame.size.height)];    _textField.placeholder = placeHolder;    _textField.secureTextEntry = secure;    [self addSubview:_textField];    [_textField release];}#pragma mark - textField 的代理- (void)setTextFieldDelegate:(id
)delegate{ _textField.delegate = delegate;}#pragma mark - 获取 textField- (UITextField *)getTextField{ return _textField;}

AppDelegate.h

@class LTView;@interface AppDelegate : UIResponder 
@property (strong, nonatomic) UIWindow *window;@property (nonatomic, retain) LTView *userName;@property (nonatomic, retain) LTView *userPWD;

AppDelegate.m

#pragma mark - 重写#pragma mark dealloc- (void)dealloc{    [_window release];    [_userName release];    [_userPWD release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //设置window    self.window = [[[UIWindow alloc] init] autorelease];    self.window.frame = [UIScreen mainScreen].bounds;    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    self.userName = [[LTView alloc] initWithFrame:CGRectMake(30, 80, 260, 40) text:@"username" placeHolder:@"请输入username" isSecure:NO];    [self.userName setTextFieldDelegate:self];    [self.window addSubview:_userName];    [_userName release];    self.userPWD = [[LTView alloc] initWithFrame:CGRectMake(30, 140, 260, 40) text:@"password" placeHolder:@"请输入password" isSecure:YES];    [self.userPWD setTextFieldDelegate:self];    [self.window addSubview:_userPWD];    [_userPWD release];    // Override point for customization after application launch.    return YES;}- (BOOL)textFieldShouldReturn:(UITextField *)textField{    if (textField == [self.userName getTextField]) {        [[self.userName getTextField] resignFirstResponder];        [[self.userPWD getTextField] becomeFirstResponder];    } else {        [textField resignFirstResponder];    }    return YES;}

转载于:https://www.cnblogs.com/jzssuanfa/p/7061465.html

你可能感兴趣的文章
springboot 端口号
查看>>
使用AChartEngine画动态曲线图
查看>>
安卓项目五子棋代码详解(四)
查看>>
urllib 学习一
查看>>
bzoj4196 [Noi2015]软件包管理器——树链剖分
查看>>
kafka源码阅读环境搭建
查看>>
UI设计
查看>>
androidtab
查看>>
Windows Phone 自定义弹出框和 Toast 通知
查看>>
如何生成静态页面的五种方案
查看>>
php 事件驱动 消息机制 共享内存
查看>>
剑指offer 二叉树的bfs
查看>>
LeetCode Maximum Subarray
查看>>
让我们再聊聊浏览器资源加载优化
查看>>
underscore demo
查看>>
CSS hack
查看>>
C# Enum Name String Description之间的相互转换
查看>>
PHP wamp server问题
查看>>
Spring Data Redis学习
查看>>
js闭包理解案例-解决for循环为元素注册事件的问题
查看>>