Skip to content

Instantly share code, notes, and snippets.

@cocoajin
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocoajin/e5fd9e7a980d7173cffa to your computer and use it in GitHub Desktop.
Save cocoajin/e5fd9e7a980d7173cffa to your computer and use it in GitHub Desktop.
限制AlertView输入框输入的字数
// 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