UI03_LTView

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

#import "AppDelegate.h"
#import "LTView.h"

@interface AppDelegate ()<UIAlertViewDelegate>

@end

@implementation AppDelegate
- (void)dealloc {
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];

//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"圣堂武士" message:@"圣罗兰" delegate:nil cancelButtonTitle:@"你猜" otherButtonTitles:@"不知道", nil];
//    //  在提示框上出现输入框, 可以设置输入框的样式
//    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
//    [alert show];

    //  用LTView来创建视图

    LTView *view = [[LTView alloc] initWithFrame:CGRectMake(1, 50, 375, 100)];
    view.backgroundColor = [UIColor yellowColor];
    [self.window addSubview:view];
    [view release];
    view.myLabel.text = @"用户名";
    view.myTextfield.placeholder = @"请输入长度为10的用户名";

    return YES;
}

LTView.h

#import <UIKit/UIKit.h>

@interface LTView : UIView<UITextFieldDelegate>
//  因为LTView里的label和textfield需要在外面设置一些内容, 所以需要在.h文件里把他们设置成属性
@property(nonatomic, retain)UILabel *myLabel;
@property(nonatomic, retain)UITextField *myTextfield;

@end

LTView.m

#import "LTView.h"

@implementation LTView

//  重写initWithFrame
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //  创建label和textfield
        [self createView];
    }
    return self;
}
- (void)createView {
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 150, 50)];
    self.myLabel.layer.borderWidth = 1;
    self.myLabel.layer.cornerRadius = 10;
    //  把label贴在LTView上
    [self addSubview:self.myLabel];
    [_myLabel release];

    //  textField
    self.myTextfield = [[UITextField alloc] initWithFrame:CGRectMake(180, 20, 150, 50)];
    self.myTextfield.layer.cornerRadius = 10;
    self.myTextfield.layer.borderWidth = 1;
    [self addSubview:self.myTextfield];
    [_myTextfield release];
    self.myTextfield.clearButtonMode = UITextFieldViewModeAlways;

    //  点击return回收键盘
    self.myTextfield.delegate = self;


}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.myTextfield resignFirstResponder];
    return YES;
}

//  别忘了给属性执行dealloc方法
- (void)dealloc {
    [_myTextfield release];
    [_myLabel release];
    [super dealloc];
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值