block使用总结

作者: Tang_shuya | 来源:发表于2016-04-21 16:21 被阅读141次

    1.block使用步骤:

    1>.有两个控制器,主动传值或者提供方法的控制器A,接受值的控制器B
    2>.让A控制器typedef void(^myBlock)(),然后copy一个myBlock类型的属性:
    @property(nonatomic ,copy) myBlock block;
    3>.在B控制器中创建A控制器,然后调用A控制器的方法
    4>.在A控制器方法中,记录block:self.block = block;
    5>.在合适的地方调用block:self.block(poiResultList.poiInfoList);
    6>.调用以后block参数里边已经有值,这个时候在B控制器中调用A控制器的方法中参数block开始执行,并且block中参数也已经有值.

    2.block原理篇:

    1>.谁要传值,谁声明和定义block以及把block作为参数的方法
    2>.传值方记录下来block,在合适的时候调用
    3>.接受值方,调用方法,等传值方调用block的时候才会在代码块中拿到block传过来的参数值.

    3.demo

    #import"TSYBaiduTool.h"

       @interfaceTSYBaiduTool()
       @property(nonatomic,copy) Blockblock;
       /** poi检索对象*/
       @property(nonatomic,strong)BMKPoiSearch*searcher;
      @end
      
      @implementationTSYBaiduTool
      single_implementation(TSYBaiduTool)
      - (void)getPoiWithCeneter:(CLLocationCoordinate2D)centerkey:(NSString*)key Result:(Block)block{
      //记录block self.block=block; 
     //初始化检索对象 _searcher=[[BMKPoiSearch alloc] init]; 
      _searcher.delegate=self;  
     //发起检索
      BMKNearbySearchOption*option = [[BMKNearbySearchOption alloc] init]; 
      option.pageIndex=0;   option.pageCapacity=20; option.location= center; option.keyword= key; 
      BOOLflag = [_searcherpoiSearchNearBy:option]; 
      if(flag){NSLog(@"周边检索发送成功");}else
      {NSLog(@"周边检索发送失败");}  
    

    #import"TSYBaiduTool.h"

       @interfaceTSYBaiduTool()
       @end
       @implementationTSYBaiduTool
       - (void)addAnnotationCenter:(CLLocationCoordinate2D)centertitle:(NSString*)title subTitle:(NSString*)subTile toMapView:(BMKMapView*)mapView{
       //添加一个PointAnnotation 
       BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc] init];
       annotation.coordinate= center; 
       annotation.title= title; 
       annotation.subtitle= subTile; 
       [mapViewaddAnnotation:annotation];  }
    

    #import"TSYBaiduTool.h"

       @interfaceTSYBaiduTool()
       @end
       @implementationTSYBaiduTool
       #pragmamark - BMKPoiSearchDelegate  
       //实现PoiSearchDeleage处理回调结果 
        -(void)onGetPoiResult:(BMKPoiSearch*)searcherresult:(BMKPoiResult*)poiResultListerrorCode:(BMKSearchErrorCode)error{ 
        if(error ==BMK_SEARCH_NO_ERROR) {
          //在此处理正常结果    
        self.block(poiResultList.poiInfoList);
        } else if(error ==BMK_SEARCH_AMBIGUOUS_KEYWORD){
         //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表 
          //result.cityList;  NSLog(@"起始点有歧义");  }else{ 
           NSLog(@"抱歉,未找到结果--%zd", error);} 
         }
       @end
    

    #import"Singleton.h"

       #import typedef void(^Block) (NSArray*result);
       @interfaceTSYBaiduTool :NSObject
       single_interface(TSYBaiduTool)
       -(void)getPoiWithCeneter:(CLLocationCoordinate2D)centerkey:(NSString*)key Result:(Block)block;
       -(void)addAnnotationCenter:(CLLocationCoordinate2D)centertitle:(NSString*)title subTitle:(NSString*)subTile toMapView:(BMKMapView*)mapView;
        @end
    

    #import"ViewController.h"

      #import"TSYBaiduTool.h"
      @interfaceViewController()
      @property(weak,nonatomic) IBOutlet BMKMapView*mapView;
      @end
    
     @implementationViewController
     - (void)viewDidLoad {
     [super viewDidLoad];
     self.mapView.delegate=self;
     }
     #pragmark - BMKMapViewDelegate
     /**
    *长按地图时调用
    *  @param mapView    地图
    *  @param coordinate点对应的经纬度
    */
     -(void)mapview:(BMKMapView*)mapView onLongClick:(CLLocationCoordinate2D)coordinate{
    //调整地图显示区域
     BMKCoordinateSpanspan = BMKCoordinateSpanMake(0.075418,0.051922);
     BMKCoordinateRegionregion = BMKCoordinateRegionMake(coordinate, span);
      [mapViewsetRegion:regionanimated:YES];
      //poi检索
       [[TSYBaiduToolsharedTSYBaiduTool]getPoiWithCeneter:coordinatekey:@"小吃"Result:^(NSArray *result) {     
     [resultenumerateObjectsUsingBlock:^(BMKPoiInfo*_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {
    //添加大头针
       [[TSYBaiduToolsharedTSYBaiduTool]addAnnotationCenter:obj.pttitle:obj.namesubTitle:obj.addresstoMapView:self.mapView];
    }];
    }];
    }
    @end

    相关文章

      网友评论

      本文标题:block使用总结

      本文链接:https://www.haomeiwen.com/subject/rnutrttx.html