簡體   English   中英

UIAlert輸入中的UITextField無法正常運行iOS 7

[英]UITextField In UIAlert Input Not Working iOS 7

我當前正在使用以下命令使用彈出警報,該警報允許用戶設置延遲(以秒為單位);

輸入本身可以正常工作,但數字不會被記住,因此,如果我輸入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=@"";
        }
    }

}

從iOS 7開始,您無法再將子視圖添加到UIAlertView - iOS7中的UIAlertView addSubview

但是,如果您只需要在UIAlertView簡單的UITextField ,則可以將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
    }
}

}

@Guferos的答案是絕對正確的。 它不起作用,因為iOS 7不支持此功能。

但是,如果您需要的不僅僅是簡單的TextView,則可以使用諸如https://github.com/jdg/MBProgressHUD之類的框架

:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM