UIAlertView - 如果未输入数据,则显示红色

时间:2013-03-08 11:20:46

标签: iphone objective-c xcode uialertview

我有一个保存按钮,点击后会显示一个警告框。我希望用户在框中输入一些数据,如果他/她没有输入任何数据,请按确定我想制作警报框文本字段红色即表示其强制性。目前我正在展示另一个警报框。请帮助我,谢谢。 以下是代码。

-(void) saveCalculaterData{

  UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:REFERENCE_ID
                                                 message:@"\n\n"
                                                delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"OK", nil];

  textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
  textField.tag=REFERENCE_FIELD;
  [textField setBackgroundColor:[UIColor whiteColor]];
  [textField setPlaceholder:ADD_REFERENCE_ID];
  [prompt addSubview:textField];



  [prompt show];


  [textField becomeFirstResponder];
  textField.delegate=self;
  }

 - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
 {
  if(buttonIndex > 0) {
        NSString *textValue = textField.text;
    if ([textField.text length] <= 0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"No Reference"
                                                       message: @"msg"
                                                      delegate: self
                                             cancelButtonTitle:@"Cancel"
                                             otherButtonTitles:nil,nil];


        [alert show];

    }

    else{
         CalculatorData *calcData = [self getCalcData];
         calcData.dataKey = textValue;
 if([database saveCalculatorData:(CalculatorData *)calcData tableName:[database Table]]){
         [Utils showAlert:RECORD_SAVED];
        }
     }
   }

 }

3 个答案:

答案 0 :(得分:0)

你试过这个吗?

textField.textColor = [UIColor redColor];

答案 1 :(得分:0)

你可以做的只是找到带有以下代码的alertView的textField实例

UITextField *alertTextField = (UITextField*)[alertView viewWithTag:REFERENCE_FIELD];

然后只需将代码显示为使用此代码将其颜色显示为红色

[alertTextField setBackgroundColor:[UIColor redColor]];

这可以解决你的问题:)

答案 2 :(得分:0)

试试这个:

- (BOOL) alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    UITextField *textField1 = [alertView textFieldAtIndex:0];
    [textField1 setDelegate:self];
    NSString *inputText = [[alertView textFieldAtIndex:0]text];
    if ([inputText length] == 7) // If it's 7 chars long, enable the button.
    {
        NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]; // Only allow numbers

        if ([inputText rangeOfCharacterFromSet:set].location != NSNotFound) { //Not a Number?

            //show alert view here
            textField1.backgroundColor = [UIColor redColor];
            alertView.message = @"Only enter numbers please!";
            return NO;
        }
        alertView.message = @"Fill in your number:";
        return YES;

    } else {
        alertView.message = @"Fill in your number:";
        textField1.backgroundColor = [UIColor clearColor];
        return NO;
    }

}

您可以根据自己的需要进行调整