iOS Method of Passing Values Between Two Pages through block

  • 2021-07-18 09:09:57
  • OfStack

1. Functional requirements

There are 1 button and 1 label on the first page, and "haha" is displayed on label by default. Click button to enter the second page. There are 1 UITextField and 1 button2 on the second page. Click button2 to return to the first page, but at the same time, the text displayed on label on the first page is changed to the text just written in UITextField.

2. Define block first

Define the method with the block parameter on the page to pass the value, that is, in the. h file on page 2:


 To redefine: typedef void (^ReturnTextBlock)(NSString *showText);

//Redefine block class name void return value type ReturnTextBlock class name (rename class name) NSString * showText parameter


 Declaration 1 A block Variables: @property (nonatomic, copy) ReturnTextBlock returnTextBlock;

//Note: The copy attribute is declared here, because block1 is placed on the stack at first, and will only be placed on the heap after copy.


block Call method of: - (void)returnText:(ReturnTextBlock)block;

Implement in. m file


block The call method implementation code of: - (void)returnText:(ReturnTextBlock)block {

self.returnTextBlock = block;

}

At this point, the preparation of block has been completed.

3. Passing values between two pages via block

On Page 1, click the button button to jump to Page 2 and call the block method on Page 2.


-(void)FirstBtnPressed

{

// Use blockSelf Modify blockSelf.label  Avoid block Inside the block _label Referenced cyclically 

__weak ViewController *blockSelf = self;

ShowViewController *orderVC=[[ShowViewController alloc]init];

//block Return value ( It's quite similar to the proxy writing , Is that the grammar is different , If the agent is here, it is self.delegate=self;)

[orderVC returnText:^(NSString *showText) {

blockSelf.label.text=showText;

}];

[ self presentViewController:orderVC animated: YES completion:nil];

}

Method of implementing button2 button in page 2. m file


-(void)SecondBtnPressed

{

// Just add before the variable __block , in block You can modify the value of the variable. Of course, there are other methods such as adding static Wait. 

[self dismissViewControllerAnimated:YES completion:^{

// In use block Need to be right before block The pointer is judged to be empty. 

// Use it directly without judging emptiness, 1 If the pointer is empty, it will directly crash. 

if (self.returnTextBlock != nil) {

self.returnTextBlock(self.text.text);

NSLog(@"text==%@",self.text.text);

}

}];

}

In this way, we can achieve the functions we want to achieve, which is very simple.

Summary

Whoever wants to pass a value defines a method containing the parameters of block, calls blcok inside the method, gives the parameters to be passed to blcok, and blcok jumps to the 'Place' where it wants to execute the code. Value passing is complete

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: