如果 uitextfield 在用户与其交互时具有特定字符串,如何编辑 uitextfield 的文本
我正在开发一个表格。因此,当用户单击提交按钮时,我检查是否所有 uitextfield 都已填充:
-(BOOL)checkUITextField{
BOOL flag = YES;
if(cognome.text.length==0){
flag=NO;
cognome.backgroundColor=[UIColor redColor];
cognome.text=@"CAMPO OBBLIGATORIO";
}else if(indirizzo.text.length==0){
flag=NO;
indirizzo.backgroundColor=[UIColor redColor];
indirizzo.text=@"CAMPO OBBLIGATORIO";
}else if(citta.text.length==0){
flag=NO;
citta.backgroundColor=[UIColor redColor];
citta.text=@"CAMPO OBBLIGATORIO";
}
}
现在,如果用户单击背景为红色和字符串“CAMPO OBBLIGATORIO”的 uitextfield 之一,我想清除文本并将颜色设置为白色。 我尝试使用 uitextfielddelegate
并实现 textFieldShouldBeginEditing
检查这一点:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if([textField.text isEqualToString:@"CAMPO OBBLIGATORIO"]){
textField.text=@"";
textField.backgroundColor=[UIColor whiteColor];
}
}
什么也没发生..
所以我尝试添加一个 ibaction 并在 xib 中将其添加到每个 uitextfield 但什么也没有发生:( 我能做什么做?
i'm developing a form. So when user click on submit button, i check if all uitextfield are filled:
-(BOOL)checkUITextField{
BOOL flag = YES;
if(cognome.text.length==0){
flag=NO;
cognome.backgroundColor=[UIColor redColor];
cognome.text=@"CAMPO OBBLIGATORIO";
}else if(indirizzo.text.length==0){
flag=NO;
indirizzo.backgroundColor=[UIColor redColor];
indirizzo.text=@"CAMPO OBBLIGATORIO";
}else if(citta.text.length==0){
flag=NO;
citta.backgroundColor=[UIColor redColor];
citta.text=@"CAMPO OBBLIGATORIO";
}
}
Now, if user click on one of the uitextfield with background red and string "CAMPO OBBLIGATORIO", i want to clear text and set back color to white.
i try using uitextfielddelegate
and implementing textFieldShouldBeginEditing
checking this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if([textField.text isEqualToString:@"CAMPO OBBLIGATORIO"]){
textField.text=@"";
textField.backgroundColor=[UIColor whiteColor];
}
}
Nothing happens..
so i try adding an ibaction and in xib adding it to every uitextfield but nothing happens :( What can i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚用你的逻辑模拟了一个测试,它应该工作得很好。我的猜测是您尚未分配 UITextField 委托。在
Interface Builder
中,您需要按住 Ctrl 键并单击UITextField
并将其拖出到您想要充当委托的对象。我可能会考虑更改检查是否需要清除 UITextField 的方式。如果您稍后对应用进行国际化,则根据用户界面字符串 (
@"CAMPO OBBLIGATORIO"
) 检查可能会中断。I just mocked up a test using your logic and it should work just fine. My guess is that you have not assigned the
UITextField
delegate. InInterface Builder
you need to ctrl+click on yourUITextField
and drag out to the object you want to act as delegate.I would probably consider changing how you check if you need to clear the
UITextField
. Checking against a user interface string (@"CAMPO OBBLIGATORIO"
) will likely break if you i18nize your app later.