自定义视图

//  AppDelegate.m


#import "AppDelegate.h"

#import "LTView.h"

@interface AppDelegate ()


@end


@implementation AppDelegate


-(void)dealloc{

    [_window release];

    [super dealloc];

}


//   创建自定义视图的步骤

    //1,创建基于UIView的视图LTView

    //2,LTView.h中声明子视图为属性

    //3,释放子视图的两个属性

    //4,重写初始化方法

    //5,懒加载创建  self.titleLable self.inputTextField

    //6,AppDelegate.m中调用自定义视图


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


    

     //6,AppDelegate.m中调用自定义视图

    

    //单行布局,用自定义视图(LTView)

   

    

    //多行布局,用自定义视图(LTView),for循环

    NSArray*titles=@[@[@"用户名",@"请输入用户名"],@[@"密码",@"请输入密码"],@[@"确认密码",@"再次输入密码"],@[@"邮箱",@"请输入邮箱"]];

    for (int i=0; icount; i++) {

        

        NSArray*title=titles[i];

        LTView*aView=[[LTView alloc]initWithFrame:CGRectMake(0, 100+50*i, 0, 0)];

        aView.backgroundColor=[UIColor colorWithRed:arc4random()%6/255.0 green:arc4random()%6/255.0 blue:arc4random()%6/255.0 alpha:0.8];

        aView.titleLable.text=title[0];

        aView.inputTextField.placeholder=title[1];

        

        [self.window addSubview:aView];

    }

    

    

    return YES;

}


//  LTView.h


#import


@interface LTView : UIView


//  设计自定义视图时声明子视图为属性,是为了方便将来实例化对象后,依然可以通过属性访问到其子视图,为子视图的相关属性赋值,例如titleLabletext属性


@property(nonatomic,retain)UILabel*titleLabel;

@property(nonatomic,retain)UITextField*inputTextField;


@end



//  LTView.m

#import "LTView.h"


@interface LTView()

{

    CGRect _frame;

}

@end


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

        

        //创建Lable

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

        

        self.inputTextField.borderStyle=UITextBorderStyleRoundedRect;

        self.inputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;

        

        [self addSubview:self.inputTextField];

         

    }

    return _inputTextField;

}







@end






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值