Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

UITextField常用设置小结

$
0
0

1、UITextField的初始化和基本设置

 myTextField = [[UITextField alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 240.0f, 30.0f)];
    [myTextField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
    myTextField.placeholder =@"test"; //默认显示的字
    myTextField.returnKeyType = UIReturnKeyDone; //什么类型的回车按钮
    //清空输入的字符模式,设为永不显示清空按钮;UITextFieldViewModeWhileEditing则是在编辑状态时(及获得焦点)显示清空按钮
    myTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

设置delegate为自己,以便响应软键盘回车键事件

myTextField.delegate=self;

设置完delegate之后,响应软键盘回车键事件

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder]; //软键盘的撤回
    return YES;
}


设置为第一焦点

[textField becomeFirstResponder];


//设置UITextField右边图像
    UIImageView *imgv=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
    myTextField.rightView=imgv;
    myTextField.rightViewMode = UITextFieldViewModeAlways; //总是显示

2、捕获UITextField的变化

//因为UITextField也是UIController,所以可以通过加一个消息事件的方式来响应文本的变化
    [myTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

处理函数

- (void) textFieldDidChange:(id) sender {
    UITextField *textField = (UITextField *)sender;
    
    NSLog(@"the text is:%@",textField.text);
}

作者:iqRocket 发表于2013-4-12 17:10:26 原文链接
阅读:18 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>