计算器 UITextField

  • 判断UITextField的输入的内容不能为空:(注意:判断UITextField有没有输入字符用字符的length来判断)
    - (IBAction)calculate {
        // 1.拿到用户输入的第一个数字
        NSString *num1String = self.num1Field.text;
        if (num1String.length == 0) {
            
            /*
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"输入有误" message:@"第一个输入框不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
             */
            [self showError:@"第一个输入框不能为空"];
            
            return;
        }
        
        // 2.拿到用户输入的第二个数字
        NSString *num2String = self.num2Field.text;
        if (num2String.length == 0) {
            
            /*
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"输入有误" message:@"第二个输入框不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
             */
            [self showError:@"第二个输入框不能为空"];
            
            return;
        }

    3.展示信息的错误方法

  • 1 // 展示错误信息
    2 - (void)showError:(NSString *)error
    3 {
    4     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"输入有误" message:error delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    5     [alertView show];
    6 }

     

posted @ 2016-01-02 22:13  mshong  阅读(179)  评论(0编辑  收藏  举报