JSAlertView附带一些便于显示简单提示的方法。
例如,如果您想显示一个带有“确定”按钮的简单提示,您需要写入以下几行代码:
使用UIAlertController
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"This is a simple alert." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:okButton];
[self presentViewController:alert animated:animated completion:nil];
使用UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"This is a simple alert." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
但使用JSAlertView会更简短、简单
[JSAlertView alert:@"This is a simple alert."];
OR
Alert(@"This is a simple alert.");
非常简单,对吧?
另一个方便的方法是询问用户问题,就像确认对话框一样
[JSAlertView confirm:@"Did you like this alert?" withCompletionHandler:^(BOOL accepted) {
NSLog(@"%@", (accepted ? @"YES" : @"NO"));
}];
最后,您可以使用以下方法进行完全自定义
[JSAlertView alert:@"Alert with multiple buttons." withTitle:@"Alert Title" buttons:@[@"Button 1",@"Button 2",@"Button 3",@"Button 4",@"Button 5"] withCompletionHandler:^(NSInteger buttonIndex, NSString *buttonTitle) {
NSLog(@"Pressed %@ at index %ld", buttonTitle, (long)buttonIndex);
}];
JSAlertView可以用于任何iOS版本,而不用担心任何iOS版本的UIAlertView支持。
使用它 将“pod 'JSAlertView'”添加到Podfile中,或者将“JSAlertView.h”和“JSAlertView.m”复制到项目中。