您的位置:首页 > 移动开发 > IOS开发

IOS复合设计模式

2015-08-11 11:37 316 查看

//LTView.h文件

@interface LTView : UIView<UITextFieldDelegate>

@property (nonatomic,retain)UILabel * label;

@property (nonatomic,retain)UITextField * textField;

//自定义初始化方法

-(instancetype)initWithPlaceholder:(NSString *)placeHolder;

//自定义初始化方法(尺寸)

-(instancetype)initWithFrame:(CGRect)frame andPlaceHoder:(NSString *)placeholder;

@end

//LTView.m文件

@implementation LTView

-(void)dealloc{

[_label release];

[_textField release];

[super dealloc];

}

-(instancetype)initWithPlaceholder:(NSString *)placeHolder{

CGRect frme = CGRectMake(0, 0, 375, 40);

self = [super initWithFrame:frme];

if (self) {

//label

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 5, frme.size.width/3.0, 30)];

self.label = label;//外界可以访问

[self addSubview:label];//self指的是视图对象,因为减号方法

[label release];

//textfield

UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(50+frme.size.width/3.0, 3, frme.size.width/3.0*2, 30)];

textField.borderStyle = UITextBorderStyleRoundedRect;

textField.placeholder = placeHolder;

self.textField = textField;

[self addSubview:textField];

[textField release];

}

return self;

}

//自定义初始化方法(尺寸)

-(instancetype)initWithFrame:(CGRect)frame andPlaceHoder:(NSString *)placeholder{

self = [super initWithFrame:frame];

if (self) {

_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, frame.size.width/3.0, 30)];

_label.backgroundColor = [UIColor orangeColor];

[self addSubview:_label];

// [_label release];

_textField = [[UITextField alloc] initWithFrame:CGRectMake(frame.size.width/3.0, 5, frame.size.width/3.0*2, 30)];

_textField.placeholder = placeholder;

_textField.borderStyle = UITextBorderStyleLine;

_textField.backgroundColor = [UIColor blueColor];

_textField.delegate = self;

[self addSubview:_textField];

[_textField release];

}

return self;

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

[textField resignFirstResponder];

return YES;

}

//AppDelegate.m文件

@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]];

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

// LTView * lt1 = [[LTView alloc] initWithPlaceholder:@"请输入用户名"];

//

// lt1.label.text = @"用户名";

// [self.window addSubview:lt1];

// [lt1 release];

LTView * lt2 = [[LTView alloc]initWithFrame:CGRectMake(0, 50, 375, 40) andPlaceHoder:@"用户"];

lt2.label.text = @"名字";

[self.window addSubview:lt2];

[lt2 release];

LTView * lt3 = [[LTView alloc]initWithFrame:CGRectMake(0, 100, 375, 40) andPlaceHoder:@"密码"];

lt3.label.text = @"密码";

//修改颜色

lt3.textField.backgroundColor = [UIColor redColor];

[self.window addSubview:lt3];

[lt3 release];

return YES;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: