KRNAlert 是 UIAlertController 的封装,简化了典型情况下的使用
KRNAlert 的所有方法都是静态的,因此您可以直接调用任何方法,而无需分配 KRNAlert 类实例。每个方法都需要您传递一个 UIViewController 的实例,从该实例中弹出警报。
要显示警报视图,您可以使用以下便捷方法之一:
+ (void)alertOKFrom:withTitle:message:completion:; // alert with custom title, message and one button with name "OK".
+ (void)alertOKCancelFrom:withTitle:message:completion:; //alert view with custom title, message and two buttons with name "OK" and "Cancel". Completion block is called if "OK" button pressed
+ (void)alertYesNoFrom:withTitle:message:yesCompletion:noCompletion:; //alert view with custom title, message and two buttons with name "YES" and "NO".
+ (void)alertErrorFrom:withMessage:completion:; //alert view with title "ERROR", custom message and "OK" Button
例如,如果您在 View Controller 实例中按下某个按钮,KRNAlert 的使用可能如下所示:
- (IBAction)buttonPressed:(id)sender {
[KRNAlert alertOKFrom:self withTitle:@"ALERT" message:@"Everything is OK" completion:^{
NSLog(@"OK Button pressed"); // completion will be called if button "OK" is pressed.
}];
}
要显示包含一个或两个按钮、任意标题、消息以及按钮标题的警报,可以使用以下方法:
+ (void)alertFrom:withTitle:message:firstButtonTitle:firstButtonCompletion:secondButtonTitle:secondButtonCompletion:;
示例
- (IBAction)customAlertPressed:(id)sender {
[KRNAlert alertFrom:self withTitle:@"Some title" message:@"Some message" firstButtonTitle:@"First Button" firstButtonCompletion:nil secondButtonTitle:@"Second button" secondButtonCompletion:nil]; // in this example completions are nil but in real app you probably would pass blocks which handle tapping on first of second button.
}
要显示包含您希望显示的任意数量按钮的警报,请使用接受UIAlertViewAction 实例数组的函数:
+ (void)alertFrom:withTitle:message:andActions:;
下面是带有五个按钮的警报示例
- (IBAction)fiveButtonsActionSheet:(id)sender {
NSMutableArray<UIAlertAction *> *actions = [NSMutableArray new];
NSArray<NSString *> *names = @[@"One", @"Two", @"Three", @"Four", @"Five"];
for (NSString *name in names) {
UIAlertAction *action = [UIAlertAction actionWithTitle:name style:UIAlertActionStyleDefault handler:nil];
[actions addObject:action];
}
[KRNAlert actionSheetFrom:self withTitle:@"Five buttons action" message:@"Here is five buttons" andActions:[actions copy]];
}
还有一个方法可以显示包含一个 UITextField 实例的警报并处理输入的文本:
+ (void)alertOKCancelFrom:withTitle:message:textFieldPlaceholder:textFieldText:secureEntry:completion:;
下面的示例对于包含用户密码输入字段的警报来说可能很有用:
- (IBAction)alertWithTextField:(id)sender {
[KRNAlert alertOKCancelFrom:self withTitle:@"Custom alert with textfield" message:@"Custom message" textFieldPlaceholder:@"Password" textFieldText:nil secureEntry:YES completion:^(NSString *textFieldString) {
NSLog(@"Password is %@", textFieldString);
}];
}
显示操作表的方法类似于显示警报视图的方法。
以下示例展示了如何使用便捷的方法来显示用于选择照片的操作表:
- (IBAction)photoActionSheetPressed:(id)sender {
[KRNAlert actionSheetFrom:self pickPhotofromGallery:^{
NSLog(@"Gallery pressed"); // here move user to his photo gallery
} andCamera:^{
NSLog(@"Camera pressed"); // here check if camera is available and move user to UIImagePickerController or custom photo picker
}];
}
要显示包含一个或两个按钮、必要的“取消”按钮以及任意标题、消息和按钮标题的操作表,可以使用以下方法:
+ (void)actionSheetFrom:withTitle:message:firstButtonTitle:firstButtonCompletion:secondButtonTitle:secondButtonCompletion:;
要显示包含任意数量按钮的操作表,请使用接受UIAlertViewAction 实例数组的函数:
+ (void)actionSheetFrom:withTitle:message:andActions:;
您可以使用以下方法重定义默认警报按钮标题:
+ (void)setOKButtonTitle:;
+ (void)setCancelButtonTitle:;
+ (void)setYes:andNoButtonTitles:;
+ (void)setErrorTitle:;
+ (void)setPhotoActionSheetTitle:andMessage:;
+ (void)setPhotoActionSheetGalleryButtonTitle:andCameraButtonTitle:;
KRNAlert 在 MIT 许可下发布。有关详细信息,请参阅 LICENSE
有任何建议或问题?请创建 GitHub 问题或联系我。