AAPLAlertViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. File: AAPLAlertViewController.m
  3. Abstract: The view controller that demonstrates how to use UIAlertView.
  4. Version: 2.12
  5. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
  6. Inc. ("Apple") in consideration of your agreement to the following
  7. terms, and your use, installation, modification or redistribution of
  8. this Apple software constitutes acceptance of these terms. If you do
  9. not agree with these terms, please do not use, install, modify or
  10. redistribute this Apple software.
  11. In consideration of your agreement to abide by the following terms, and
  12. subject to these terms, Apple grants you a personal, non-exclusive
  13. license, under Apple's copyrights in this original Apple software (the
  14. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  15. Software, with or without modifications, in source and/or binary forms;
  16. provided that if you redistribute the Apple Software in its entirety and
  17. without modifications, you must retain this notice and the following
  18. text and disclaimers in all such redistributions of the Apple Software.
  19. Neither the name, trademarks, service marks or logos of Apple Inc. may
  20. be used to endorse or promote products derived from the Apple Software
  21. without specific prior written permission from Apple. Except as
  22. expressly stated in this notice, no other rights or licenses, express or
  23. implied, are granted by Apple herein, including but not limited to any
  24. patent rights that may be infringed by your derivative works or by other
  25. works in which the Apple Software may be incorporated.
  26. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  27. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  28. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  29. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  30. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  31. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  32. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  35. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  36. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  37. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  38. POSSIBILITY OF SUCH DAMAGE.
  39. Copyright (C) 2014 Apple Inc. All Rights Reserved.
  40. */
  41. #import "AAPLAlertViewController.h"
  42. // Corresponds to the row in the alert view section.
  43. typedef NS_ENUM(NSInteger, AAPLAlertsViewControllerTableRow) {
  44. AAPLAlertsViewControllerAlertViewRowSimple = 0,
  45. AAPLAlertsViewControllerAlertViewRowOkayCancel,
  46. AAPLAlertsViewControllerAlertViewRowOther,
  47. AAPLAlertsViewControllerAlertViewRowTextEntry,
  48. AAPLAlertsViewControllerActionSheetRowTextEntrySecure
  49. };
  50. @interface AAPLAlertViewController() <UIAlertViewDelegate>
  51. @end
  52. #pragma mark -
  53. @implementation AAPLAlertViewController
  54. // Show an alert with an "Okay" button.
  55. - (void)showSimpleAlert {
  56. NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
  57. NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
  58. NSString *cancelButtonTitle = NSLocalizedString(@"OK", nil);
  59. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
  60. [alert show];
  61. }
  62. // Show an alert with an "Okay" and "Cancel" button.
  63. - (void)showOkayCancelAlert {
  64. NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
  65. NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
  66. NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
  67. NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);
  68. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle, nil];
  69. [alert show];
  70. }
  71. // Show an alert with two custom buttons.
  72. - (void)showOtherAlert {
  73. NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
  74. NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
  75. NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
  76. NSString *otherButtonTitleOne = NSLocalizedString(@"Choice One", nil);
  77. NSString *otherButtonTitleTwo = NSLocalizedString(@"Choice Two", nil);
  78. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitleOne, otherButtonTitleTwo, nil];
  79. [alert show];
  80. }
  81. // Show a text entry alert with two custom buttons.
  82. - (void)showTextEntryAlert {
  83. NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
  84. NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
  85. NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
  86. NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);
  87. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle, nil];
  88. alert.alertViewStyle = UIAlertViewStylePlainTextInput;
  89. [alert show];
  90. }
  91. // Show a secure text entry alert with two custom buttons.
  92. - (void)showSecureTextEntryAlert {
  93. NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
  94. NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
  95. NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
  96. NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);
  97. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle, nil];
  98. alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
  99. [alert show];
  100. }
  101. #pragma mark - UIAlertViewDelegate
  102. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  103. if (alertView.cancelButtonIndex == buttonIndex) {
  104. NSLog(@"Alert view clicked with the cancel button index.");
  105. }
  106. else {
  107. NSLog(@"Alert view clicked with button at index %ld.", (long)buttonIndex);
  108. }
  109. }
  110. - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
  111. // Enforce a minimum length of >= 5 characters for secure text alert views.
  112. if (alertView.alertViewStyle == UIAlertViewStyleSecureTextInput) {
  113. return [[alertView textFieldAtIndex:0].text length] >= 5;
  114. }
  115. return YES;
  116. }
  117. #pragma mark - UITableViewDelegate
  118. // Determine the action to perform based on the selected cell.
  119. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  120. AAPLAlertsViewControllerTableRow row = indexPath.row;
  121. switch (row) {
  122. case AAPLAlertsViewControllerAlertViewRowSimple:
  123. [self showSimpleAlert];
  124. break;
  125. case AAPLAlertsViewControllerAlertViewRowOkayCancel:
  126. [self showOkayCancelAlert];
  127. break;
  128. case AAPLAlertsViewControllerAlertViewRowOther:
  129. [self showOtherAlert];
  130. break;
  131. case AAPLAlertsViewControllerAlertViewRowTextEntry:
  132. [self showTextEntryAlert];
  133. break;
  134. case AAPLAlertsViewControllerActionSheetRowTextEntrySecure:
  135. [self showSecureTextEntryAlert];
  136. break;
  137. default:
  138. break;
  139. }
  140. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  141. }
  142. @end