@property(nonatomic ,retain)UILabel *myLabel;
@property(nonatomic ,retain)UITextField *myTextField;因为要在类的外部获取输入框的内容,修改label的标题,所以我们可以把这两部分作为属性写在.h中,这样在外部就可以直接进行修改
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        // 模块化(同样功能的代码放到一起)
        [self createView ];   
    }
    return self;
}/*
   创建两个子视图,一个是label.
 */
self.myLabel =[[UILabel alloc] initWithFrame:CGRectMake(50, 20, 100, 30)];
self.myLabel.backgroundColor =[UIColor yellowColor];
[self addSubview:self.myLabel];
[_myLabel release];
/*
    一个是textfield
 */
self.myTextField =[[UITextField alloc] initWithFrame:CGRectMake(200, 20, 100, 30)];
self.myTextField.backgroundColor =[UIColor cyanColor];
[self addSubview:self.myTextField];
// 设置代理人
self.myTextField.delegate =self;
[_myTextField release];    
} 
-(void)dealloc 
{ 
    [_myTextField release]; 
    [_myLabel release]; 
    [super dealloc]; 
}
在签订协议设置代理人之后就可以实现这个方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}例子:
#import "LTView.h"例子:
@property(nonatomic ,retain)LTView *ltView;实现更改 label里的文字 
例子:
self.ltView =[[LTView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
    self.ltView.backgroundColor =[UIColor redColor];
    [self.window addSubview:self.ltView];
    [self.ltView release];
    self.ltView.myLabel.text =@"姓名";一个控件 
(1).在.m文件中添加一个属性 
代码例子:
@property(nonatomic,retain)UIAlertView *alertView;(2).实现UIAlertView的控件:
self.alertView=[[UIAlertView alloc] initWithTitle:@"测试" message:@"试试" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    // 让alertView中出现textfield   self.alertView.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput; 
    实现方法
[self.alertView  show];(3).添加方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"11");
    // 先找到alertView中的textfield
    UITextField *first =[self.alertView textFieldAtIndex:0];
    NSLog(@"%@",first.text);
    if(buttonIndex == 0){
        NSLog( @"取消按钮");
    }
    if (buttonIndex == 1) {
        NSLog(@"确定按钮");
    }效果图 
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/mltianya/article/details/47175137