一个使用基于块的代理而不是协议的 iOS Alert View。
pod 'MTBlockAlertView'
。MTBlockAlertView.h
和 MTBlockAlertView.m
从 MTBlockAlertView
target 中复制到您的项目中。不遵守 UIAlertViewDelegate
协议,而是使用块
void (^completionHandler)(UIAlertView *, NSInteger) = ^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex == alertView.cancelButtonIndex) {
[MTBlockAlertView showWithTitle:@"Cancel" message:@"You tapped Cancel"];
} else {
[MTBlockAlertView showWithTitle:@"OK" message:@"You tapped OK"];
}
};
MTBlockAlertView *alertView = [[MTBlockAlertView alloc] initWithTitle:@"Init Version"
message:@"You tapped the init version"
completionHanlder:completionHandler
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
除了不必遵守协议的便利之外,您还有每个 MTBlockAlertView
实例负责其自己的消失的优点。尝试在同一个视图控制器中使用多个 UIAlertView
的代理方法的人都知道这种感觉。
我们还为常见的使用情况提供了几个类便利方法。运行 MTBlockAlertViewDemo
target 以查看一些示例。
[MTBlockAlertView showWithTitle:@"Cool Message" message:@"This is a really cool message"];
[MTBlockAlertView showWithTitle:@"Cool Message"
message:@"Tapping this will do something cool"
completionBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
// Do some work
}];
[MTBlockAlertView showWithTitle:@"Cool Message"
message:@"I like blocks."
cancelButtonTitle:@"Cancel"
otherButtonTitle:@"OK"
alertViewStyle:UIAlertViewStylePlainTextInput
completionBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
// Do some work
}];
[Parker Wightman)(https://github.com/pwightman) (@parkerwightman)