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;
}
相关文章推荐
- 关于iOS7 上下黑条解决办法
- iOS多线程编程之Grand Central Dispatch(GCD)
- iOS字符串大小转换
- iOS 9应用开发教程之ios9中实现按钮的响应
- iOS创建调试证书真机调试
- ios中assign、copy 、retain、strong、weak的区别
- GitHub发布Ipa文件|GitHub发布plist文件|GitHub发布IOS应用
- iOS跳转界面时隐藏tabBar的方法:hidesBottomBarWhenPushed
- GCD的使用与理解(2)
- iOS 9应用开发教程之ios9中实现按钮的响应
- iOS蓝牙开发入门04--服务和特征
- iOS 9 分屏多任务(3):画中画(PiP)快速入门
- iOS播放音乐文件时的操作和打断处理
- 9秒学院iOS游戏开发从掌握到拥有,你只需四步
- iOS 9 分屏多任务(2):Slide Over & Split View快速入门
- iOS开发:创建真机调试证书
- IOS开发笔记之常用的第三方库(太多了。)
- IOS视频播放器VKVideoPlayer
- iOS中.m中,@interface部分的作用
- IOS开发 @property中assign、copy 、retain等关键字的理解