0

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

-(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];
        }
     }
   }

 }
4

3 回答 3

0

你试过这个吗?

textField.textColor = [UIColor redColor];
于 2013-03-08T11:33:40.020 回答
0

你可以做的就是用下面的代码找到 alertView 的 textField 的实例

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

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

[alertTextField setBackgroundColor:[UIColor redColor]];

愿这能解决你的问题:)

于 2013-03-08T11:34:48.940 回答
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;
    }

}

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

于 2013-03-08T11:36:17.047 回答