IOS复合设计模式

//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;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值