// AppDelegate.m
#import "AppDelegate.h"
#import "LTView.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-(void)dealloc{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//视图控制器
MainViewController*mainVC=[[[MainViewController alloc]init]autorelease];
self.window.rootViewController=mainVC;
return YES;
}
// LTView.h
#import
@interface LTView : UIView
//属性
@property(nonatomic,retain)UILabel*titleLable;
@property(nonatomic,retain)UITextField*inputTextField;
@end
// LTView.m
#import "LTView.h"
@implementation LTView
//3
-(void)dealloc{
[_titleLable release];
[_inputTextField release];
[super dealloc];
}
//4
-(instancetype)initWithFrame:(CGRect)frame{
frame=CGRectMake(0, frame.origin.y, CGRectGetWidth([[UIScreen mainScreen]bounds]), 40);
self=[super initWithFrame:frame];
if (self) {
}
return self;
}
//5懒加载
-(UILabel *)titleLable{
if (!_titleLable) {
CGFloat width=(CGRectGetWidth(self.frame)-50)/7;
self.titleLable=[[[UILabel alloc]initWithFrame:CGRectMake(30, 5, width*2, 30)]autorelease];
[self addSubview:self.titleLable];
}
return _titleLable;
}
-(UITextField *)inputTextField{
if (!_inputTextField) {
CGFloat width=(CGRectGetWidth(self.frame)-50)/7;
self.inputTextField=[[[UITextField alloc]initWithFrame:CGRectMake(self.titleLable.frame.origin.x+CGRectGetWidth(self.titleLable.frame)+5, 5, width*5, 30)]autorelease];
_inputTextField.borderStyle=UITextBorderStyleRoundedRect;
_inputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
[self addSubview:_inputTextField];
}
return _inputTextField;
}
@end
// MainViewController.m
#import "MainViewController.h"
#import "LTView.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray*titles=@[@[@"用户名",@"请输入用户名"],
@[@"密码",@"请输入密码"],
@[@"确认密码",@"再次输入密码"]];
for (int i=0; icount; i++) {
NSArray*title=titles[i];
LTView*aView=[[[LTView alloc]initWithFrame:CGRectMake(0, 100+50*i, 0, 0)]autorelease];
aView.titleLable.text=title[0];
aView.inputTextField.placeholder=title[1];
aView.inputTextField.borderStyle=UITextBorderStyleRoundedRect;
aView.inputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
[self.view addSubview:aView];
}
// LTView*aView=[[[LTView alloc]initWithFrame:CGRectMake(0, 100, 0, 0)] autorelease];
// aView.titleLable.text=@"用户名:";
// aView.inputTextField.placeholder=@"请输入用户名";
// aView.tag=123;
// [self.view addSubview:aView];
}