iOS base pass-through inverse value (delegate, block)

Source: Internet
Author: User

For the benefit of novice iOS!

The values are often used in the project, and are divided into CIS (from the root controller to the sub-controller) and the inverse (from the sub-controller to the root controller) according to the direction of the pass value. I wrote a demo here to illustrate the effect and create two controllers:

One for the root controller and one for the child controller.

Shun Chuan: This is the simplest way to pass the value, add an attribute in the sub-controller.

The following is a OtherViewController.h file

#import <UIKit/UIKit.h>@interface  otherviewcontroller:uiviewcontroller/* * * *pushstring;    @end

In the root controller, set the incoming value when jumping

    Otherviewcontroller *OTHERVC = [[Otherviewcontroller alloc] init];     @" strings passed in from the root controller " ;    [Self.navigationcontroller pushviewcontroller:othervc Animated:yes];

This allows you to get the incoming value in the child controller.

Reverse Pass (delegate mode):

Here is the OtherViewController.h file, adding protocol methods and proxies.

#import <UIKit/UIKit.h>@protocol otherviewcontrollerdelegate <nsobject>-(void ) popvcwithstring: (NSString *) popstring; @end @interface Otherviewcontroller:uiviewcontroller /*  */IDdelegate; @end

When you need to pass a value, use the following code to send out the value you want to pass:

if ([Self. Delegate respondstoselector: @selector (popvcwithstring:)]) {        [self. delegate popvcwithstring:@ " string returned by proxy from the child controller "];    }

This time it needs to be set in the root controller (VIEWCONTROLLER.M), set the proxy:

    Otherviewcontroller *OTHERVC = [[Otherviewcontroller alloc] init];     // set up proxy    OTHERVC. delegate = self ;    [Self.navigationcontroller pushviewcontroller:othervc Animated:yes];

Compliance with agency agreements:

// compliance with agency agreements @interface Viewcontroller () <OtherViewControllerDelegate>

To implement the Proxy method:

// Proxy Method -(void) popvcwithstring: (NSString *) popstring{    //  Get the value of the inverse pass here    NSLog (@ "popstring-----%@", popstring);}

Reverse Pass (block mode):

To declare a block in a sub-controller (OTHERVIEWCONTROLLER.H), add a block property

#import <UIKit/UIKit.h>void(^popstringblock) (NSString *popstring); @interface Otherviewcontroller:uiviewcontroller /*  */@property (nonatomic, copy) Popstringblock Popblock; @end

When you need to pass a value, use the following code to send out the value you want to pass:

Self.popblock (@ " string returned from the sub-controller via block ");

At this point, you only need to call block in the root controller (VIEWCONTROLLER.M) where you need to take a value:

    Otherviewcontroller *OTHERVC = [[Otherviewcontroller alloc] init];     = ^ (NSString *popstring) {        //  get block mode inverse value        NSLog (@ "popstring----- %@", popstring);    };    [Self.navigationcontroller pushviewcontroller:othervc Animated:yes];

Writing is relatively simple, the key places are attached to the code, do not understand can go to see my demo:https://github.com/sjxjjx/delegate_block.

iOS base pass-through inverse value (delegate, block)

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.