uialertview block 回调

来源:互联网 发布:搅拌机品牌 知乎 编辑:程序博客网 时间:2024/06/06 15:42

最近项目中需要对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];        }    }


其实现在很多代码中都换成了block,相比代理来说,看到别人这样分析的。

公共接口,方法较多也选择用delegate进行解耦 
iOS有很多例子比如最常用tableViewDelegate,textViewDelegate 

异步和简单的回调用block更好 
iOS有很多例子比如常用的网络库AFNetwork,ASIHTTP库,UIAlertView类。 

都是为了在具体的实现Delegate或Block中再进行处理,属于策略模式,具体算法在具体算法类中实现。

0 0