Three ways to recall common iOS

Source: Internet
Author: User

Recognize the three common callback modes for iOS.

Proxy mode is the most common mode of communication in iOS, and proxies are almost ubiquitous.





Here is an array where we first pass the array to the other methods through the proxy method.

Set up protocols and methods

    1. @protocol callbackdelegate;
    2. @interface Viewcontroller:uiviewcontroller
    3. @property (Weak, nonatomic) id<callbackdelegate> delegate;
    4. @end
    5. @protocol callbackdelegate <NSObject>
    6. -(void) Showarraywithdelegate: (Nsarray *) array;
    7. @end

@interface Viewcontroller () <CallBackDelegate>


Click the button to pass an array to display it

    1. -(Ibaction) Delegatecallback
    2. {
    3. Nsdictionary *dict = @{@ "array": @[@ "Chelsea", @ "MUFC", @ "Real Madrid"]};
    4. Nsarray *array = dict[@ "Array"];
    5. [Self.delegate Showarraywithdelegate:array];
    6. }

Called, displays

    1. -(void) Showarraywithdelegate: (Nsarray *) array
    2. {
    3. _outputlabel.text = array[2];
    4. }

The most important is also the most easy to ignore, is to set the point of delegate.
Screen display after completion




Using notification hubs
Notification hubs can be used without setting up proxies, but you need to set up observers and remove observers.




Code

    1. -(Ibaction) CallBack
    2. {
    3. Nsdictionary *dict = @{@ "array": @[@ "Chelsea", @ "MUFC", @ "Real Madrid"]};
    4. Nsarray *array = dict[@ "Array"];
    5. [[Nsnotificationcenter Defaultcenter] postnotificationname:@ "outputarraynotification" Object:array];
    6. }

Registering and removing the viewer

    1. -(void) Viewwillappear: (BOOL) animated
    2. {
    3. [Super viewwillappear:animated];
    4. [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (outputwithnote:) name:@ " Outputarraynotification "Object:nil";
    5. }
    6. -(void) Viewdiddisappear: (BOOL) animated
    7. {
    8. [Super viewdiddisappear:animated];
    9. [[Nsnotificationcenter Defaultcenter] removeobserver:self name:@ "outputarraynotification" object:nil];
    10. }

Show

    1. -(void) Outputwithnote: (nsnotification *) anotification
    2. {
    3. Nsarray *receivearray = [Anotification object];
    4. _outputlabel.text = receivearray[0];
    5. }





Block
What is block: The blocks syntax from the C declarator to the Objective-c
Block codes pass various content in the form of closures, which can be code, which can be an array of everything. Block code is very convenient to unify the code in different places, making it more readable.


See how this is done with array passing.

typedef void (^arr_block) (Nsarray *array);

    1. -(void) Showarrayusingblock: (Arr_block) Block
    2. {
    3. Nsdictionary *dict = @{@ "array": @[@ "Chelsea", @ "MUFC", @ "Real Madrid"]};
    4. Nsarray *array = dict[@ "Array"];
    5. Block (array);
    6. }

Call method, display

    1. -(Ibaction) Blockcallback
    2. {
    3. [Self showarrayusingblock:^ (Nsarray *array) {
    4. _outputlabel.text = array[1];
    5. }];
    6. }

>

Three ways to recall common iOS

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.