码迷,mamicode.com
首页 > 其他好文 > 详细

封装代理

时间:2015-07-03 00:19:46      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:封装   代理   方法   uilabel   cgrect   

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<UITextFieldDelegate>)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<UITextFieldDelegate>)delegate
{
    _textField.delegate = delegate;
}



#pragma mark - 获取 textField
- (UITextField *)getTextField
{
    return _textField;
}

AppDelegate.h

@class LTView;

@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>

@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:@"用户名" placeHolder:@"请输入用户名" isSecure:NO];
    [self.userName setTextFieldDelegate:self];
    [self.window addSubview:_userName];
    [_userName release];



    self.userPWD = [[LTView alloc] initWithFrame:CGRectMake(30, 140, 260, 40) text:@"密码" placeHolder:@"请输入密码" 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

封装代理

标签:封装   代理   方法   uilabel   cgrect   

原文地址:http://blog.csdn.net/yadong_zhao/article/details/46732677

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!