code block values are passed from the back to the forward value
1. Declaration code block (SECONDXXX.H)
2. Declaring a code block type's properties (SecondXXX.h)
3. Calling code block (SECONDXXX.M)
4. Implementing code blocks (SECONDXXX.M)
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface Appdelegate:uiresponder <UIApplicationDelegate>
@property (Strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
@interface Appdelegate ()
@end
@implementation Appdelegate
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Firstviewcontroller *first=[[firstviewcontroller alloc] init];
Self.window.rootviewcontroller=first;
return YES;
}
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface firstviewcontroller:uiviewcontroller<uitextfielddelegate>
@property (strong,nonatomic) Uitextfield *mytext;
@property (strong,nonatomic) UIButton *mybutton;
@end
#import "FirstViewController.h"
@interface Firstviewcontroller ()
@end
@implementation Firstviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
Self.view.backgroundcolor=[uicolor colorwithpatternimage:[uiimage imagenamed:@ "44.jpg"];
Self. Mytext=[[uitextfield Alloc] Initwithframe:cgrectmake (100, 100, 200, 50)];
Self. mytext.borderstyle=1;
Self. mytext.delegate=self;
Self. Mytext.tintcolor=[uicolor Redcolor];
[Self.view addsubview:self. MyText];
Self. Mybutton=[[uibutton Alloc] Initwithframe:cgrectmake (150, 200, 100, 50)];
Self. Mybutton.backgroundcolor=[uicolor Redcolor];
[Self. MyButton settitle:@ "Next" forstate:uicontrolstatenormal];
[Self. MyButton addtarget:self Action: @selector (NextPage) forcontrolevents:uicontroleventtouchupinside];
[Self.view addsubview:self. MyButton];
}
-(void) NextPage
{
Secondviewcontroller *second=[[secondviewcontroller alloc] init];
Assigning the contents of a text box to a string
Second. Mystr=self. Mytext.text;
4. Implementing code block Display information
Second. myblock=^ (NSString *infor)
{
Self. Mytext.text=infor;
};
Skip to Next page
[Self presentviewcontroller:second animated:yes completion:
^{
NSLog (@ "Message delivery success");
}];
}
Hide keyboard
-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
{
if ([TextField Isfirstresponder])
{
[TextField Resignfirstresponder];
}
return YES;
}
#import <UIKit/UIKit.h>
1. Declaration code block (SECONDXXX.H) forward value
typedef void (^postvalueblock) (NSString *infor);
#import "FirstViewController.h"
@interface secondviewcontroller:uiviewcontroller<uitextfielddelegate>
@property (strong,nonatomic) NSString *mystr;
@property (strong,nonatomic) Uitextfield *mytext;
2. Declaring a code block type's properties (SecondXXX.h)
@property (strong,nonatomic) Postvalueblock Myblock;
@end
#import "SecondViewController.h"
@interface Secondviewcontroller ()
@end
@implementation Secondviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
Add a background map to the parent view
Self.view.backgroundcolor=[uicolor colorwithpatternimage:[uiimage imagenamed:@ "20130714160426_rPTnU.jpeg"];
Initialize text box
Self. Mytext=[[uitextfield Alloc] Initwithframe:cgrectmake (100, 100,200,50)];
Self. mytext.borderstyle=1;
Self. mytext.delegate=self;
Assigning a string to a text box
Self. Mytext.text=self. MyStr;
Add a text box on the parent view
[Self.view addsubview:self. MyText];
}
3. Methods for calling code blocks
-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
{
3.1 Calling Block
if (self. Myblock)
{
Self. Myblock (self. Mytext.text);
}
3.2 Hide Keyboard
if ([TextField Isfirstresponder])
{
[TextField Resignfirstresponder];
}
3.3 Back to previous page
[Self Dismissviewcontrolleranimated:yes completion:
^{
NSLog (@ "Previous page");
}];
return YES;
}
Method code block for parameter passing between IOS page and page