UIAextField在UIAlert输入不能正常工作iOS 7

时间:2014-07-07 21:47:44

标签: ios objective-c ios7

我目前正在使用以下内容来使用弹出警报,该警报允许用户以秒为单位设置延迟;

输入本身工作正常,但数字不会被记住,所以如果我按下5或10,当我按下'设置'它只是忽略了这个价值并再次进入即时录制状态。这里是调用setDelay的IBAction,在它下面,我发布了Alertview

-(IBAction)setDelay:(id)sender
{
    // open a alert with text field,  OK and cancel button
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Set delay in seconds" message:@" "
                                                   delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Set", nil];





    CGRect frame = CGRectMake(14, 45, 255, 23);
    if(!delayTextField) {
        delayTextField = [[UITextField alloc] initWithFrame:frame];

        delayTextField.borderStyle = UITextBorderStyleBezel;
        delayTextField.textColor = [UIColor blackColor];
        delayTextField.textAlignment = UITextAlignmentLeft;
        delayTextField.font = [UIFont systemFontOfSize:14.0];

        delayTextField.backgroundColor = [UIColor redColor];
        delayTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support

        delayTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;  // use the default type input method (entire keyboard)
        delayTextField.returnKeyType = UIReturnKeyDefault;
        delayTextField.delegate = self;
        delayTextField.clearButtonMode = UITextFieldViewModeWhileEditing;   // has a clear 'x' button to the right
    }
    else 
    {
        delayTextField.text=@"";
    }

    alert.delegate=self;
    [alert addSubview:delayTextField];
    [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];

    [alert show];
    [alert release]; 
}

//

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==100) 
    {
        if (buttonIndex!=alertView.cancelButtonIndex)
        {
            [self featureButtonPressed];
        }

    }
    else 
    {
        if (buttonIndex==alertView.cancelButtonIndex)
        {
            self.delayTextField.text=@"";
        }
    }

}

2 个答案:

答案 0 :(得分:0)

从iOS 7开始,您无法再将子视图添加到UIAlertView - UIAlertView addSubview in iOS7

但是,如果您在UITextField中只需要简单的UIAlertView,则只需将UIAlertView样式设置为UIAlertViewStylePlainTextInput,然后再从该字段中检索值。例如

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Set delay in seconds" message:@" "
                                               delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Set", nil];

alert.delegate=self;
[alert addSubview:delayTextField];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];

然后在你的委托方法中:

 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag==100) 
{
    if (buttonIndex!=alertView.cancelButtonIndex)
    {
        [self featureButtonPressed];
    }

}
else 
{
    if (buttonIndex==alertView.cancelButtonIndex)
    {
        UITextField *alertViewTextField = [alertView textFieldAtIndex:0];
        //Do something with alertViewTextField.text value
    }
}

}

答案 1 :(得分:-1)

@Guferos答案是绝对正确的。它不起作用,因为iOS 7不支持此功能。

但是如果您需要的不仅仅是简单的TextView,您可以使用https://github.com/jdg/MBProgressHUD等框架

:)