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)