美文网首页
TextField 与TextView

TextField 与TextView

作者: 冯娇王 | 来源:发表于2016-11-24 11:12 被阅读0次

TextField 方法

键盘的问题,请使用第三方解决,高效,牛X!

1.让textField只能输入数字

```

#define NUMBERS @"0123456789\n"

-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{

NSCharacterSet*cs;

if(textField ==self.accNo|| textField ==self.numberText)

{

        cs = [[NSCharacterSetcharacterSetWithCharactersInString:NUMBERS]invertedSet];

        NSString*filtered = [[stringcomponentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];

        BOOLbasicTest = [stringisEqualToString:filtered];

        if(!basicTest)

           {

               UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:@"提示"

             message:@"请输入数字"

             delegate:nil

             cancelButtonTitle:@"确定"

             otherButtonTitles:nil];

             [alertshow];

           returnNO;

            }

}

      returnYES;  

}

```

2. 通知的来监听[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(textFieldChange)name:UITextViewTextDidChangeNotificationobject:nil];

3.textfield加一个删除按钮

UIView*rightVeiw = [[UIViewalloc]initWithFrame:CGRectMake(0, searIV.frame.size.height/2-10,30,20)];

UIImageView* xImageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"ic_delete-0"]];

xImageView.frame=CGRectMake(10,3,14,14);

xImageView.userInteractionEnabled=YES;

[rightVeiwaddSubview:xImageView];

searchTF.rightView= rightVeiw;

searchTF.rightViewMode=UITextFieldViewModeWhileEditing;

UITapGestureRecognizer*xImageViewTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(xImageViewClick:)];

[xImageViewaddGestureRecognizer:xImageViewTap];


4.这个方法也可以拿到变化的textField后的值

[_titleTextFieldaddTarget:selfaction:@selector(valueChanted:)forControlEvents:UIControlEventAllEditingEvents];

相关文章

网友评论

      本文标题:TextField 与TextView

      本文链接:https://www.haomeiwen.com/subject/tmsbpttx.html