用于针对所有 iOS 版本的 UIAlertView / UIActionSheet / UIAlertController 的封装类。
RMUniversalAlert 是一个封装类,基于 UIAlertView+Blocks、UIActionSheet+Blocks 和 UIAlertController+Blocks 构建,并为所有 iOS 版本提供了一个简化的界面。
typedef void(^RMUniversalAlertCompletionBlock)(RMUniversalAlert *alert, NSInteger buttonIndex);
+ (instancetype)showAlertInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSArray *)otherButtonTitles
tapBlock:(RMUniversalAlertCompletionBlock)tapBlock;
+ (instancetype)showActionSheetInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSArray *)otherButtonTitles
popoverPresentationControllerBlock:(void(^)(RMPopoverPresentationController *popover))popoverPresentationControllerBlock
tapBlock:(RMUniversalAlertCompletionBlock)tapBlock;
以下代码将在所有 iOS 版本上显示一个 _alert 视图,并允许您在一个单独的代码路径中执行您的逻辑。在 iOS 8 和更高版本上,它将使用 UIAlertController - 在破坏性按钮上提供红色文本。在 iOS 7 和更早版本上,它将使用标准的 UIAlertView。
[RMUniversalAlert showAlertInViewController:self
withTitle:@"Test Alert"
message:@"Test Message"
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Delete"
otherButtonTitles:@[@"First Other", @"Second Other"]
tapBlock:^(RMUniversalAlert *alert, NSInteger buttonIndex){
if (buttonIndex == alert.cancelButtonIndex) {
NSLog(@"Cancel Tapped");
} else if (buttonIndex == alert.destructiveButtonIndex) {
NSLog(@"Delete Tapped");
} else if (buttonIndex >= alert.firstOtherButtonIndex) {
NSLog(@"Other Button Index %ld", (long)buttonIndex - alert.firstOtherButtonIndex);
}
}];
RMUniversalAlert.showAlertInViewController(self,
withTitle: "Test Alert",
message: "Test Message",
cancelButtonTitle: "Cancel",
destructiveButtonTitle: "Delete",
otherButtonTitles: ["First Other", "Second Other"],
tapBlock: {(alert, buttonIndex) in
if (buttonIndex == alert.cancelButtonIndex) {
println("Cancel Tapped")
} else if (buttonIndex == alert.destructiveButtonIndex) {
println("Delete Tapped")
} else if (buttonIndex >= alert.firstOtherButtonIndex) {
println("Other Button Index \(buttonIndex - alert.firstOtherButtonIndex)")
}
})
iOS 6 及更高版本。从版本 0.7 开始,头文件使用了新的 Objective-C nullability 注释 以便更好地与 Swift 交互,所以您将需要 Xcode 6.3 或更高版本来编译它。
以下代码将在所有 iOS 版本上显示操作表,并允许您在一个单独的代码路径中执行您的逻辑。在 iOS 8 及更高版本上,它将使用 UIAlertController - 在破坏性按钮上提供红色文本。在 iOS 7 及更早版本上,它将使用标准 UIActionSheet。
popoverPresentationControllerBlock
允许您在操作表是在 iPad 上时配置折叠视图的源视图/矩形/工具栏按钮项。
[RMUniversalAlert showActionSheetInViewController:self
withTitle:@"Test Action Sheet"
message:@"Test Message"
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Delete"
otherButtonTitles:@[@"First Other", @"Second Other"]
popoverPresentationControllerBlock:^(RMPopoverPresentationController *popover){
popover.sourceView = self.view;
popover.sourceRect = sender.frame;
}
tapBlock:^(RMUniversalAlert *alert, NSInteger buttonIndex){
if (buttonIndex == alert.cancelButtonIndex) {
NSLog(@"Cancel Tapped");
} else if (buttonIndex == alert.destructiveButtonIndex) {
NSLog(@"Delete Tapped");
} else if (buttonIndex >= alert.firstOtherButtonIndex) {
NSLog(@"Other Button Index %ld", (long)buttonIndex - alert.firstOtherButtonIndex);
}
}];
RMUniversalAlert.showActionSheetInViewController(self,
withTitle: "Test Action Sheet",
message: "Test Message",
cancelButtonTitle: "Cancel",
destructiveButtonTitle: "Delete",
otherButtonTitles: ["First Other", "Second Other"],
popoverPresentationControllerBlock: {(popover) in
popover.sourceView = self.view
popover.sourceRect = sender.frame
},
tapBlock: {(alert, buttonIndex) in
if (buttonIndex == alert.cancelButtonIndex) {
println("Cancel Tapped")
} else if (buttonIndex == alert.destructiveButtonIndex) {
println("Delete Tapped")
} else if (buttonIndex >= alert.firstOtherButtonIndex) {
println("Other Button Index \(buttonIndex - alert.firstOtherButtonIndex)")
}
})
使用 CocoaPods 运行 pod 'RMUniversalAlert'
。
下载此项目,导航到 Tests
并运行 pod install
。打开 RMUniversalAlert.xcworkspace
。