iOS 第三课

来源:互联网 发布:小程序php源码下载 编辑:程序博客网 时间:2024/06/13 11:32

#import <UIKit/UIKit.h>@interface LTView : UIView#pragma mark - 声明方法- (instancetype)initWithFrame:(CGRect)frame                         text:(NSString *)text                  placeHolder:(NSString *)placeHolder;#pragma mark 设置输入框为密文方式- (void)setSecureTextEntry:(BOOL)isSecureTextEntry;#pragma mark 设置输入框提示文字- (void)setTextFieldPlaceHolder:(NSString *)placeHolder;#pragma mark 对输入框中的值赋值取值的方法- (void)setTextFieldText:(NSString *)text;- (NSString *)textFieldText;#pragma mark 对标签显示的文字赋值取值的方法- (void)setLabelText:(NSString *)text;- (NSString *)labelText;#pragma mark 设置输入框的代理- (void)setTextFieldDelegate:(id<UITextFieldDelegate>)delegate;#pragma mark 通过一个方式,让输入框释放第一响应者- (void)recycleKeyBoard;@end

#import "LTView.h"@interface LTView ()//延展#pragma mark - 声明自己特有的属性@property (nonatomic, retain, readonly) UILabel *label;@property (nonatomic, retain, readonly) UITextField *textField;@end@implementation LTView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {            }    return self;}#pragma mark - 声明方法- (instancetype)initWithFrame:(CGRect)frame                         text:(NSString *)text                  placeHolder:(NSString *)placeHolder{    if (self = [super initWithFrame:frame]) {                // 创建UILabel和UITextField并添加上        CGFloat width = frame.size.width;        CGFloat height = frame.size.height;                _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width * 0.3, height)];        _label.textAlignment = NSTextAlignmentCenter;        [self addSubview:_label];                _textField = [[UITextField alloc] initWithFrame:CGRectMake(width * 0.3, 0, width - width * 0.3, height)];        _textField.borderStyle = UITextBorderStyleRoundedRect;        [self addSubview:_textField];                        // 将传递过来的参数赋值到UIlabe和UITextField上        _label.text = text;        _textField.placeholder = placeHolder;    }    return self;}#pragma mark 设置输入框为密文方式- (void)setSecureTextEntry:(BOOL)isSecureTextEntry{    _textField.secureTextEntry = isSecureTextEntry;}#pragma mark 设置输入框提示文字- (void)setTextFieldPlaceHolder:(NSString *)placeHolder{    _textField.placeholder = placeHolder;}#pragma mark 对输入框中的值赋值取值的方法- (void)setTextFieldText:(NSString *)text{    _textField.text = text;}- (NSString *)textFieldText{    return _textField.text;}#pragma mark 对标签显示的文字赋值取值的方法- (void)setLabelText:(NSString *)text{    _label.text = text;}- (NSString *)labelText{    return _label.text;}#pragma mark 设置输入框的代理- (void)setTextFieldDelegate:(id<UITextFieldDelegate>)delegate{    _textField.delegate = delegate;}#pragma mark 通过一个方式,让输入框释放第一响应者- (void)recycleKeyBoard{    [_textField resignFirstResponder];}#pragma mark - 重写#pragma mark dealloc- (void)dealloc{    [_label release];    [_textField release];        [super dealloc];}@end


#import "AppDelegate.h"#import "LTView.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];                    // 创建LTView    LTView *view1 = [[LTView alloc] initWithFrame:CGRectMake(20, 50, 280, 30) text:@"用户名" placeHolder:@"请输入用户名"];    [_window addSubview:view1];    [view1 release];            LTView *view2 = [self createLTViewWithFrame:CGRectMake(20, 100, 280, 30) text:@"密码" placeHolder:@"请输入密码"];    [_window addSubview:view2];        LTView *view3 = [self createLTViewWithFrame:CGRectMake(20, 150, 280, 30) text:@"密码" placeHolder:@"请输入密码"];    [_window addSubview:view3];        UIButton *button1 = [self createButtonWithFrame:CGRectMake(20, 200, 280, 35) title:@"button1"];    [_window addSubview:button1];        UIButton *button2 = [self createButtonWithFrame:CGRectMake(20, 250, 280, 35) title:@"button1"];    [_window addSubview:button2];        UIButton *button3 = [self createButtonWithFrame:CGRectMake(20, 300, 280, 35) title:@"button1"];    [_window addSubview:button3];            return YES;}- (UIButton *)createButtonWithFrame:(CGRect)frame                              title:(NSString *)title{    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];    button.frame = frame;    button.backgroundColor = [UIColor redColor];    [button setTitle:title forState:UIControlStateNormal];    return button;}- (LTView *)createLTViewWithFrame:(CGRect)frame                             text:(NSString *)text                      placeHolder:(NSString *)placeHolder{    LTView *view = [[LTView alloc] initWithFrame:frame text:text placeHolder:placeHolder];    return [view autorelease];}#pragma mark button点击事件- (void)buttonAction:(UIButton *)sender{    LTView *ltview = (LTView *)[_window viewWithTag:1000];    NSLog(@"%@", [ltview textFieldText]);        [ltview recycleKeyBoard];}//UItextFieldShoulddeleage 代理- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return YES;}


0 0
原创粉丝点击