Unable to add UITextField to UIAlertView on iOS7…works in iOS 6

后端 未结 7 1512
梦毁少年i
梦毁少年i 2020-12-01 01:34

The code below works on iOS6 (and before) but the UITextField does not display in iOS7...any ideas on how to get a UITextField to display in an UIAlterView in iOS7?

7条回答
  •  半阙折子戏
    2020-12-01 01:46

    1) In method - (id)initWithAlertTitle:(NSString *)title checkForPassword:(NSString *)password
    you should add

    self.alertViewStyle = UIAlertViewStylePlainTextInput;
    

    sample:

    (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;
    }
    

    in method show add next

    (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];
    
    }
    

    also make changes in method click

    #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];
            }
        }
    }
    

提交回复
热议问题