Last active
August 29, 2015 14:04
-
-
Save cocoajin/e5fd9e7a980d7173cffa to your computer and use it in GitHub Desktop.
限制AlertView输入框输入的字数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Show a secure text entry alert with two custom buttons. | |
- (void)showSecureTextEntryAlert { | |
NSString *title = NSLocalizedString(@"A Short Title Is Best", nil); | |
NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil); | |
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil); | |
NSString *otherButtonTitle = NSLocalizedString(@"OK", nil); | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle, nil]; | |
alert.alertViewStyle = UIAlertViewStyleSecureTextInput; | |
[alert show]; | |
} | |
#pragma mark - UIAlertViewDelegate | |
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { | |
if (alertView.cancelButtonIndex == buttonIndex) { | |
NSLog(@"Alert view clicked with the cancel button index."); | |
} | |
else { | |
NSLog(@"Alert view clicked with button at index %ld.", (long)buttonIndex); | |
} | |
} | |
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { | |
// Enforce a minimum length of >= 5 characters for secure text alert views. | |
if (alertView.alertViewStyle == UIAlertViewStyleSecureTextInput) { | |
return [[alertView textFieldAtIndex:0].text length] >= 5; | |
} | |
return YES; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment