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;
}
在方法显示添加下一个
(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];
}
}
}