即使从UIDatePicker中选择日期,UIAlertView按钮也会被禁用

时间:2014-05-29 10:11:34

标签: ios7 uialertview uidatepicker uialertviewdelegate

问题是从UIDatePicker中选择日期后,UIAlertView的“确定”按钮被禁用。我试过,但没有运气。

-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView 
每当我们在textfield中键入任何内容时都应该调用

,但是当输入日期时它不会被调用,但是如果我从键盘输入任何东西,那么它就可以了。

enter image description here

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0 && indexPath.row == 1)
       {
        alertView1 = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done",nil];
        alertView1.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
        alertView1.tag=1;
        alertText = [alertView1 textFieldAtIndex:0];
        itemText = [alertView1 textFieldAtIndex:1];
                alertText.inputView=datePicker;
        itemText.inputView=secondPicker;
        [datePicker addTarget:self action:@selector(firstTF) forControlEvents:UIControlEventValueChanged];
        [secondPicker addTarget:self action:@selector(secondTF) forControlEvents:UIControlEventValueChanged];
        [alertText setPlaceholder:@"From Date"];
        [itemText setPlaceholder:@"To Date"];
        itemText.secureTextEntry = NO;
        [alertView1 show];
}

- (void)firstTF
{
    NSDate *date = datePicker.date;
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateStyle:NSDateFormatterMediumStyle];
    alertText.text = [dateFormat stringFromDate:date];
}

- (void)secondTF
 {
    NSDate *date = secondPicker.date;
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateStyle:NSDateFormatterMediumStyle];
    itemText.text = [dateFormat stringFromDate:date];
 }


- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
 {
    NSString *inputText = [[alertView textFieldAtIndex:0] text];

    if( [inputText length] > 0)
    {
         NSLog(@"alertViewShouldEnableFirstOtherButton: was called!");
         return YES;
    }
    else
    {
        return NO;
    }
}

1 个答案:

答案 0 :(得分:0)

这是因为在将文本输入到文本字段之前调用了“alertViewShouldEnableFirstOtherButton”。根据您的情况,“Done”按钮将被禁用。删除此方法,您将实现目标。