iOS--block使用總結

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中參數也已經有值.atom

2.block原理篇:

1>.誰要傳值,誰聲明和定義block以及把block做爲參數的方法
2>.傳值方記錄下來block,在合適的時候調用
3>.接受值方,調用方法,等傳值方調用block的時候纔會在代碼塊中拿到block傳過來的參數值.spa

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
相關文章
相關標籤/搜索