The use of Block reverse value transfer in iOS development

Keywords: iOS Attribute

stay Reverse value transfer through proxy in iOS development In this paper, we analyze the use of proxy mode to reverse value transfer. In fact, there are some other ways, such as notification, Block, etc. compared with proxy, I personally think it is simpler, but we need to deal with the details, such as Block circular reference. In the previous case, Block is used for implementation this time. The basic knowledge of Block will not be covered in this article.

I. writing standard

For Block value transfer, it should be noted that whoever transfers the value needs to define the Block. The capture party only needs to transfer the Block to the value transfer party and process the captured value.

  • Value passing party
    1. Define Block for value transfer
    2. Declare a Block attribute. The specific implementation of this attribute needs to be passed in by the catcher
    3. Call Block to complete value transfer when value transfer is needed

  • Capturing party
    1. Pass a Block to the sender
    2. Capture the transmitted value in the Block and process the captured value according to the demand

Block and reverse value transfer

The effect of the case is as follows:

Reverse pass.gif

III. implementation steps

1. Value transfer party

//.h file

/**
 *  Type customization
 */
typedef void (^ReturnValueBlock) (NSString *strValue);

@interface NextViewController : UIViewController
/**
 *  Declare a ReturnValueBlock property, which is passed in from the interface of getting the passed value
 */
@property(nonatomic, copy) ReturnValueBlock returnValueBlock;

@end

=================================================================

//.m file

#import "NextViewController.h"

@interface NextViewController ()

@property (weak, nonatomic) IBOutlet UITextField *inputText;

- (IBAction)back:(id)sender;

@end

@implementation NextViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.title = @"Second interface";
}


/**
 *  Back to previous screen
 *
 *  @param sender Button
 */
- (IBAction)back:(id)sender {

    NSString *inputString = self.inputText.text;

    __weak typeof(self) weakself = self;

    if (weakself.returnValueBlock) {
        //Pass out your own value and complete the transfer
        weakself.returnValueBlock(inputString);
    }

    [self.navigationController popViewControllerAnimated:YES];
    
}

@end

2. Capture party

//.m file

#import "ViewController.h"
#import "NextViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *nextPassedValue;

- (IBAction)next:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
}

//Click the button to jump to the second interface
- (IBAction)next:(id)sender {

    NextViewController *nvc = [[NextViewController alloc]init];
    
    //Assign Block and the captured value to UILabel
    nvc.returnValueBlock = ^(NSString *passedValue){
        
        self.nextPassedValue.text = passedValue;
    
    };
    
    [self.navigationController pushViewController:nvc animated:YES];
 
}
@end

Posted by phoenixx on Sun, 08 Dec 2019 16:03:39 -0800