LTView

创建一个LTView的类

在LTView的.h里签协议 <UITextFieldDelegate>

在LTView的.h里写属性

//因为要在类的外部获取输入框的内容,修改label的标题,所以我们可以把这两部分作为属性写在.h,这样在外部可以直接进行修改和设置

@property(nonatomic,retain)UILabel *myLabel;

@property(nonatomic,retain)UITextField *myTextField;

//重写默认的初始化方法,instancetype初始化方法时使用

-(instancetype)initWithFrame:(CGRect)frame{

    self=[super initWithFrame:frame];

    if (self) {

        //模块化

        [self creatView];

    }

    return self;

}


-(void)creatView

{   //创建两个子视图,一个是label,一个是textfield

    self.myLabel=[[UILabel alloc] initWithFrame:CGRectMake(20, 100, 50, 30)];

    self.myLabel.backgroundColor=[UIColor cyanColor];

    [self addSubview:self.myLabel];

    [self.myLabel release];

    

    self.myTextField=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];

    self.myTextField.backgroundColor=[UIColor lightGrayColor];

    [self addSubview:self.myTextField];

    //设置代理人

     self.myTextField.delegate=self;

    [self.myTextField release];

 }



//return回收键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    return YES;

}

别忘了写相应的dealloc方法

在AppDelegate.m里引头文件

#import "LTView.h"


//创建一个LTView的对象

    LTView *view=[[LTView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];

    [self.window addSubview:view];

    [view release];

    view.myLabel.text=@"姓名";

    view.myLabel.textAlignment=NSTextAlignmentCenter;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值