测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
Released最后发布 | 2015年3月 |
由 RockyZhang 维护。
此针对所有 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 版本上显示一个警报,并允许您在单行代码路径中执行您的逻辑。在 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 版本上显示一个操作表,并允许您在单行代码路径中执行您的逻辑。在 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
。