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