web-dev-qa-db-ja.com

iOS7でUITextFieldをUIAlertViewに追加できない... iOS 6で動作

以下のコードはiOS6(およびそれ以前)で動作しますが、UITextFieldはiOS7に表示されません... iOS7のUIAlterViewにUITextFieldを表示する方法に関するアイデア

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;

nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setKeyboardType:UIKeyboardTypeNumberPad];
[nameField becomeFirstResponder];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];

[nameField release];

IOS6のコード実行では、これが表示されます。

enter image description here

iOS7の同じコードはこれを表示します(UITextFieldが欠落しており、キーボードがないことに注意してください):

enter image description here

64
iTrout

IOS 7では、UIAlertViewのビュー階層を簡単に変更することはできません。

あなたの場合の一つの選択肢は、alert.alertViewStyle = UIAlertViewStylePlainTextInput;を設定することです。これにより、テキストフィールドが追加されます。 UITextField *textField = [alertView textFieldAtIndex:0];を使用して、UIAlertViewデリゲートコールバックでアクセスできます。

178
Aaron Brager

@Aaron Bragerには適切なソリューションがありました。さらに、彼の提案の後、数字キーパッドをデフォルトにする行を追加しました。

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;

dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
[dialog textFieldAtIndex:0].keyboardType = UIKeyboardTypeNumberPad;

CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
17
iTrout
UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Credit Card Number"
                                  message:@"Please enter your credit card number:"
                                  delegate:self
                                  cancelButtonTitle:@"Cancel"
                                  otherButtonTitles:@"Ok", nil];
        [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        /* Display a numerical keypad for this text field */
        UITextField *textField = [alertView textFieldAtIndex:0];
        textField.keyboardType = UIKeyboardTypeNumberPad;

[alertView show];
6
Manjit Singh

ただの魅力として働いている

IOSのすべてのバージョンのUIAlertViewの2つのUITextField

-(IBAction) showAlertView {

    UIAlertView *alert;  
    UITextField *callForwardNumber;
    UItextField *callForwardCondition;

    alert = [[UIAlertView alloc] initWithTitle:@"Enter Phone Number & Rule"
                                   message:@""
                                  delegate:self
                         cancelButtonTitle:@"Cancel"
                         otherButtonTitles:@"Save", nil];

    //alert.transform = CGAffineTransformMakeTranslation(0, 110);

    callForwardNumber = [[UITextField alloc] init];
    callForwardNumber.keyboardType = UIKeyboardTypeNumberPad;
    callForwardNumber.text = [R.prefs objectForKey:@"fwd_number"];
    callForwardNumber.borderStyle = UITextBorderStyleRoundedRect;
    callForwardNumber.delegate = self;
    callForwardNumber.tag = 1;

    callForwardCondition = [[UITextField alloc] init];
    callForwardCondition.text = callCondition;
    callForwardCondition.borderStyle = UITextBorderStyleRoundedRect;
    callForwardCondition.delegate = self;
    callForwardCondition.tag = 2;
    [callForwardCondition setKeyboardType:UIKeyboardTypeNumberPad];

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    UIView* customAccessory = 
                   [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 55)];
    callForwardNumber.frame = CGRectMake(0, 0, 245.0, 25.0);
    callForwardCondition.frame = CGRectMake(0, 30.0, 245.0, 25.0);
    [customAccessory addSubview:callForwardNumber];
    [customAccessory addSubview:callForwardCondition];
    [alert setValue:customAccessory forKey:@"accessoryView"];
    [alert show];
} else {
    alert.message = @"\n\n\n";
    [alert show];
    callForwardNumber.frame = CGRectMake(20.0, 45.0, 245.0, 25.0);
    callForwardCondition.frame = CGRectMake(20.0, 75.0, 245.0, 25.0);
    [alert addSubview:callForwardNumber];
    [alert addSubview:callForwardCondition];
}

}
1
Salim

また、cocoacontrols.comのカスタムコントロールをご覧ください。 MLAertView (UIのようなiOS 7)および TSAlertView (UIのようなiOS 6)をご覧ください。それらは回転角にも変換できます。

0
NeverHopeless

1)メソッド内-(id)initWithAlertTitle:(NSString *)title checkForPassword:(NSString *)password
追加する必要があります

self.alertViewStyle = UIAlertViewStylePlainTextInput;

サンプル:

(id)initWithAlertTitle:(NSString *)title
        checkForPassword:(NSString *)password{
     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
     {
    self.alertViewStyle = UIAlertViewStylePlainTextInput;
    }
    self = [super initWithTitle:title
                        message:@"" // password field will go here
                       delegate:self
              cancelButtonTitle:@"Cancel"
              otherButtonTitles:@"Enter", nil];
    if (self) {
        self.password = password;
        self.hashTechnique = HashTechniqueNone; // use no hashing by default
        secondMessage = @"Please Enter New Password";
        thirdMessage = @"Please Re-Enter Password";
        secondMessageNew = @"Please Enter Password";
    }

NSLog(@" _password_ %@",_password);
NSLog(@"_old_password_ %@",[[NSUserDefaults standardUserDefaults] objectForKey:kPassword]);

return self;
}

メソッドshow addで次を追加

(void)show {

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
     {

       UITextField *passwordField = [self textFieldAtIndex:0];
       passwordField.delegate = self;
      self.passwordField = passwordField;
    }
   else
   {
               UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(14, 45, 256, 25)];
               passwordField.secureTextEntry = YES;
               passwordField.placeholder = @"";
               passwordField.backgroundColor = [UIColor whiteColor];



               // Pad out the left side of the view to properly inset the text
               UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6, 19)];
                passwordField.leftView = paddingView;
                passwordField.leftViewMode = UITextFieldViewModeAlways;

           //    // Set delegate
           self.passwordField.delegate = self;

           // Set as property

            self.passwordField = passwordField;
           // Add to subview
           [self addSubview:_passwordField];
     }

    // Show alert
   [super show];

}

メソッドのクリックも変更します

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {


    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        UITextField *passwordField = [self textFieldAtIndex:0];
        self.passwordField = passwordField;
    }

    if (buttonIndex == alertView.firstOtherButtonIndex) {

        if ([self enteredTextIsCorrect] || [self.title isEqualToString:secondMessage] || [self.title isEqualToString:secondMessageNew]) {

            if (([self.title isEqualToString:secondMessage] || [self.title isEqualToString:secondMessageNew]) && (self.passwordField.text.length > 0)) {
                self.password = self.passwordField.text;
                self.title = thirdMessage;
                self.passwordField.text = @"";

                if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
                {
                    if ([self.passwordDelegate respondsToSelector:@selector(notifyParent::)]) {
                        [self.passwordDelegate notifyParent:thirdMessage:self.password];
                    }
               }
            }else
            {
                if ([self.title isEqualToString:thirdMessage]) {
                    [[NSUserDefaults standardUserDefaults] setObject:self.password forKey:kPassword];
                    [[NSUserDefaults standardUserDefaults] synchronize];

                    if (self.passwordDelegate) {
                        if ([self.passwordDelegate respondsToSelector:@selector(notifyParentWithState:)]) {
                            [self.passwordDelegate notifyParentWithState:YES];
                        }
                    }
                }else{
                    if ([self.title isEqualToString:secondMessageNew]) {
                        self.title = secondMessageNew;
                    }
                    else{
                        self.title = secondMessage;
                    }

                     self.passwordField.text = @"";
                     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
                     {
                         if ([self.passwordDelegate respondsToSelector:@selector(notifyParent::)]) {
                             [self.passwordDelegate notifyParent:self.title:self.password];
                        }
                    }
                }
            }
        }

        // If incorrect then animate
        else {
            [self animateIncorrectPassword];
        }
    }
}
0
Svitlana

私も同じ問題に直面しています。サーフィン中に答えが得られました。それがあなたにも役立つことを願っています。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Folder Name?" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.tag = 2;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex    {
    UITextField * alertTextField = [alertView textFieldAtIndex:0];
    NSLog(@"alerttextfiled - %@",alertTextField.text);
}
0
Alok kumar