gpt4 book ai didi

ios - 检查 UITextfield 是否为空并显示警报

转载 作者:行者123 更新时间:2023-11-28 11:11:59 26 4
gpt4 key购买 nike

我想阻止保存数据,并在任何文本字段留空时显示警报。这就是我所拥有的,当我点击保存按钮时弹出警报,但文本字段在为空时仍以空白文本保存。

   // check if text feild is empty

if usernameLabel.text == "" || passwordLabel.text == "" || phoneNumber.text == "" || service.text == "" || address.text == "" {

// Alert
let optionMenu = UIAlertController(title: nil, message: "Please Enter Text", preferredStyle: .Alert)

// Add actions to the menu
let cancelAction = UIAlertAction(title: "Ok", style: .Cancel, handler:
nil)
optionMenu.addAction(cancelAction)

// Display the menu
self.presentViewController(optionMenu, animated: true, completion: nil)

}


} //End save button

最佳答案

你首先删除空白,因为它也被视为字符。所以必须删除代码下面的空格,

-(NSString *)remove_whitespace:(NSString *)string_data /* Remove whitspace in the string */
{
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
string_data = [string_data stringByTrimmingCharactersInSet:whitespace];
return string_data;
}

然后,计算字符串的长度。如果大于0则继续,否则弹出如下错误,

if([self remove_whitespace:textstring])
{
NSLog(@"work");
}
else
{
NSLog(@"so error");
}

明白了吗?

关于ios - 检查 UITextfield 是否为空并显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34410778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com