0

带有 UITextField 的 UIAlertView 在 iOS 7 中不起作用。如何解决?

这是屏幕截图:

在此处输入图像描述 这是我的代码:

如何在 iOS7 的 UIAlertView 中获取 UITextFeield ?

-(void)takeUserName
{
        mChangePlayerAlert = [[UIAlertView alloc] init];
        mChangePlayerAlert.title = title; 
        mChangePlayerAlert.message = @"\n";
        mChangePlayerAlert.delegate = self;
        [mChangePlayerAlert addButtonWithTitle:@"Save"];
        [mChangePlayerAlert addButtonWithTitle:@"Cancel"];

        CGRect frame;
        frame = CGRectMake(20, 45, 245, 27);//(200, 500, 400, 120);

        mTextFeild = [[UITextField alloc] initWithFrame:frame];
        mTextFeild.textColor = [UIColor blueColor];
        mTextFeild.borderStyle = UITextBorderStyleRoundedRect;

        mTextFeild.keyboardType = inType;//;
        mTextFeild.returnKeyType = UIReturnKeyDone;
        mTextFeild.autocorrectionType = UITextAutocorrectionTypeNo;
        mTextFeild.autocapitalizationType = UITextAutocapitalizationTypeNone;
        mTextFeild.delegate = self;

        [mChangePlayerAlert addSubview:mTextFeild];

        mTextFeild.delegate = self;
        [mTextFeild becomeFirstResponder];

        [mChangePlayerAlert show];
}
4

4 回答 4

2

您不应该使用文本视图。

mChangePlayerAlert.alertViewStyle = UIAlertViewStylePlainTextInput;

要稍后获取该值,textFieldAtIndex:请在警报视图委托中使用:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex != alertView.cancelButtonIndex)
    {
        NSString* text = [alertView textFieldAtIndex:0].text;

        ...
    }
}
于 2013-09-27T05:41:25.087 回答
2

您可以改用自定义警报视图并在那里添加您想要的任何内容:自定义 iOS 7 警报视图

于 2013-09-27T06:05:33.810 回答
1

如果上面的答案不适合你试试这个

    [txtvwMessage setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

一般来说,iOS6 到 iOS7 的转换会失去设计控制。这就是为什么自动调整概念可以帮助您进行转换的原因

于 2013-09-27T06:04:45.343 回答
0

尝试这个。

- (IBAction)showAlert:(id)sender
{
    UIAlertView *alertView = [[UIAlertView alloc] init];

    switch (((UIButton *)sender).tag)
    {
    // Default Alert View
    case 1001:

        alertView.title = @"Default Alert View";
        alertView.message = @"UIAlertViewStyleDefault";
        [alertView addButtonWithTitle:@"OK"];

        break;

    // Secure Alert View
    case 1002:

        alertView.title = @"Secure Alert View";
        alertView.message = @"UIAlertViewStyleSecureTextInput";
        alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
        [alertView addButtonWithTitle:@"OK"];
        [alertView addButtonWithTitle:@"Cancel"];

        break;

    // Plain Alert View
    case 1003:

        alertView.title = @"Plain Alert View";
        alertView.message = @"UIAlertViewStylePlainTextInput";
        alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alertView addButtonWithTitle:@"OK"];
        [alertView addButtonWithTitle:@"Cancel"];

        break;

    // Login ALert View
    case 1004:

        alertView.title = @"Login Alert View";
        alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
        [alertView addButtonWithTitle:@"OK"];
        [alertView addButtonWithTitle:@"Cancel"];

        break;
   }

   [alertView show];
 }
于 2013-12-13T07:16:51.117 回答