iOS Development--Project Practical Technology OC Chapter & use block as a proxy for value transfer

Source: Internet
Author: User

Use block as a proxy for value transfer


Here http://www.cnblogs.com/iCocos/p/4659878.html, the author has the agent, Block, notification of the value of the way summed up a bit, today's use is not the same, What we're using here is to save a block code in one way, and then go back inside the Blcok to execute it.

    • Here is used in the map,,,, well don't say much, open dry

1: First, define a Blcok

void (^resultblock) (Cllocation *currentloc, Clplacemark *placemark, NSString *error);


2. Declare a method with block blocks to make it easier to call outside and pass in block code blocks

1 /* * 2 *  get current position 3  *4  *  @param block to get the current position after processing block5  */6 -(void) Getcurrentlocation: (resultblock) block;


3. Define a Blcok attribute in the implementation file, which records the block passed in the following method, and executes

// stores the code blocks passed by the outside world @property (nonatomic, copy) Resultblock Resultblock;


4. How to implement block correspondence

1 //Get Current Location2- (void) Getcurrentlocation: (resultblock) block3 {4     //Record code block5Self.resultblock =Block;6 7 8     //determine whether to turn on location services9     if([Cllocationmanager locationservicesenabled]) {Ten         //Start updating user locations One [Self.locationm startupdatinglocation]; A     } -     Else -     { theSelf.resultblock (nil, nil,@"location service is not turned on"); -     } -}


5. Determine where code blocks need to be executed and execute the external incoming block (as in the article mentioned above)

        if (self.resultblock) {            self.resultblock ([Locations lastobject], [Placemarks Firstobject], nil);        }


6, outside the use of the time directly with the use of UIView animation, in the back will attach a blcok, we just need to implement the corresponding code in this blcok, the system will automatically save the code first, and then put in the blcok inside to execute

1     [[Icocoslocationtool Sharedicocoslocationtool] getcurrentlocation:^ (cllocation *currentloc, CLPlacemark *placemark, NSString *error) {2         if0) {3             NSLog (@ " %@   ----   %@", Currentloc, placemark.name); 4         }5     }];



iOS Development--Project Practical Technology OC Chapter & use block as a proxy for value transfer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.