标签:
代码块传值 是从后往前传值
1.声明代码块 (SecondXXX.h)
2.声明一个代码块类型的属性(SecondXXX.h)
3.调用代码块(SecondXXX.m)
4.实现代码块(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];
//将文本框的内容赋给字符串
second.MyStr=self.MyText.text;
//4.实现代码块 显示信息
second.MyBlock=^(NSString *Infor)
{
self.MyText.text=Infor;
};
//跳到下一页面
[self presentViewController:second animated:YES completion:
^{
NSLog(@"信息传递成功");
}];
}
//隐藏键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isFirstResponder])
{
[textField resignFirstResponder];
}
return YES;
}
#import <UIKit/UIKit.h>
//1.声明代码块 (SecondXXX.h)往前传值
typedef void(^PostValueBlock)(NSString *Infor);
#import "FirstViewController.h"
@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property(strong,nonatomic) NSString *MyStr;
@property(strong,nonatomic) UITextField *MyText;
//2.声明一个代码块类型的属性(SecondXXX.h)
@property(strong,nonatomic) PostValueBlock MyBlock;
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//在父视图上添加背景图
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"20130714160426_rPTnU.jpeg"]];
//初始化文本框
self.MyText=[[UITextField alloc] initWithFrame:CGRectMake(100, 100,200,50)];
self.MyText.borderStyle=1;
self.MyText.delegate=self;
//将字符串赋给文本框
self.MyText.text=self.MyStr;
//在父视图上添加文本框
[self.view addSubview:self.MyText];
}
//3. 调用代码块的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//3.1 调用Block
if (self.MyBlock)
{
self.MyBlock(self.MyText.text);
}
//3.2 隐藏键盘
if ([textField isFirstResponder])
{
[textField resignFirstResponder];
}
//3.3 返回上一页面
[self dismissViewControllerAnimated:YES completion:
^{
NSLog(@"上一页");
}];
return YES;
}
标签:
原文地址:http://www.cnblogs.com/tmf-4838/p/5281491.html