There are many places to use blocks, where the value is just a small part of it, and the following is a description of the value of block between two interfaces:
Let's talk about thought:
First, create two view controllers, create a Uilabel and a uibutton in the first view controller, where Uilabel is to display the string passed by the second view controller, UIButton is to push to the second interface.
The second interface has only one Uitextfield, is to enter the text, when the input text, and return to the first interface, when the second view is going to disappear, the second interface on the textfiled text to the first interface, and displayed on the Uilabel.
In fact, the core code on a few lines of code:
Here's the main code: (Because I'm a project created with storyboard, so the above properties and the corresponding method are using the system-generated outlet)
One, declare the Block property in the. h file of the second view controller
typedef Void (^returntextblock ) (NSString *showtext); @interface Textfieldviewcontroller:uiviewcontroller@property (nonatomic, copy) Returntextb< span class= "keyword" style= "font-weight:bold" >lock returntextblock; -(void) Returntext: (Returntextblock ) block; @end
The first line of code is to redefine a name for the block to be declared.
RETURNTEXTBLock
In this way, the following will be very convenient when used.
The third line is a block property defined
Line four is a function that passes in a block statement in the first interface, but it doesn't have to be, but plus it reduces the amount of code written.
Ii. methods for implementing the second view controller
-(void)Returntext:(Returntextblock) Block { Self. Returntextblock = Block;} -(void)viewwilldisappear:(BOOL) Animated { if( Self. returntextblock! =Nil) { Self. Returntextblock ( Self. Inputtf.text); }}
Where Inputtf is the Uitextfield in the view.
The first method is to define the method, save the incoming block statement block to this class instance variable Returntextblock (. h defined in the attribute), and then look for a timing call, and this time is said above, when the view will disappear, need to rewrite:
-(void) Viewwilldisappear: (BOOL) animated;
Method.
Third, get the second view controller in the first view, and invoke the defined property with the second view controller
Write in the following ways:
- (void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{ //Get The new view controller using [Segue Destinationviewcontroller]. //Pass The selected object to the new view controller. Textfieldviewcontroller *TFVC = Segue.destinationviewcontroller; [TfVC returntext:^ (NSString *showtext) { Self.showLabel.text = Showtext; }];}
iOS Development: Use block to pass values between two interfaces (Block advanced usage: Block pass value)