最近项目中需要对uialertview的选择进行判断然后回调,因此想到了block,网上搜了搜,发现了UIAlertView-Block,觉得很方便,因此分享出来。可能许多人早就用过了,也就不详细说明了,直接上代码。
if(![str isEqual: @"javascript:;"])
{
if ([str rangeOfString:@"tel://" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
UIAlertView *aView = [[UIAlertView alloc] initWithTitle:nil
message:@"确认拨打电话:"
cancelButtonTitle:@"取消"
otherButtonTitles:@"拨打", nil];
[aView show];
[aView setHandler:^(UIAlertView *alert, NSInteger buttonIndex)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
forButtonAtIndex:1];
}
else
{
ShopDetailViewController *ctl = [[ShopDetailViewController alloc] init];
ctl.shopDetailUrl = [dict objectForKey:@"url"];
ctl.shouldHideTabbar = YES;
ctl.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:ctl animated:YES];
}
}
公共接口,方法较多也选择用delegate进行解耦
iOS有很多例子比如最常用tableViewDelegate,textViewDelegate
异步和简单的回调用block更好
iOS有很多例子比如常用的网络库AFNetwork,ASIHTTP库,UIAlertView类。
都是为了在具体的实现Delegate或Block中再进行处理,属于策略模式,具体算法在具体算法类中实现。