ios - 具有多个零时显示警报消息

标签 ios uitextfield uialertview

我有两个 UITextfields - textfield1textfield2

我只是在做两个文本字段的乘法运算。 textfield2有一个固定的值,在textfield1中用户可以自己设置值。

现在,我面临一个问题。如果用户设置值 0,那么我将显示一条警告消息。

if ([textfield1.text isEqualToString:@"0"])
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:APP_NAME message:@"You can not set Zero." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}

但是,如果用户设置了多个零或十进制零(0.0 或 0.00),那么我将无法显示警报消息。

最佳答案

不要使用字符串。转换为数字:

double value1 = [textfield1.text doubleValue];
if (value1 == 0.0) {
    // show alert
}

更新:实际上,使用doubleValue 并不是一个好主意,因为您想支持来自世界各地的用户。有些用户可能会输入 0.5 的值,而另一些用户可能会使用 0,5 等。最好使用 NSNumberFormatter 将输入的文本转换为一个数字。

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSNumber *number = [formatter numberFromString:textfield1.text];
double value1 = [number doubleValue];

关于ios - 具有多个零时显示警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16008779/

相关文章:

php - 使用 ASIFormDataRequest 将 UIImage 从 UIImagePickerController 发送到 PHP 服务器

ios - 如何修改文本字段数组中选定的 UITexFiled 值

iOS 辅助功能隐藏 "textField, double tap to edit"公告

ios - 表情符号键盘布局未正确绘制并且缺少许多表情符号

iphone - 在 iOS 中使用 AlertView

iOS - JSQMessage 气泡会在键盘模式更改时向下滚动

ios - UIImageJPEGRepresentation 增加 UIImage 大小

ios - "Import Error"具有 Transporter App 状态模式

iphone - UIAlertView 按钮到另一个 View Controller

ios - ios-应该在NSUrlConnection的返回 block 中使用UIAlertView吗?