iOS传值使用Block传值。
iOS开发使用block传值很简单,如果你对函数指针的使用非常熟练,直接使用就可以了。如果还不是了解函数指针先了解函数指针。
typedef进行声明一个Block:
声明方法: typedef void(^myBlock)(NSString *Str); void 是返回值,NSString是参数 myBlock是声明返回值为空,参数是NSString类型的类型的值。^这个东西是不是和*有点相似。
继续先声明一个Block变量: @property (nonatomic,copy)myBlock myBlockargs;
调用一下: self.myBlockargs(label.text);后面括号中的是要传给下一个页面的数据(label.text)
第一个页面调用:
myView.myBlockargs = ^(NSString * str)
{
label.text = str;
};
str中的值就是第二个页面传过来的值。
上源代码:第一个页面
.m文件
#import "ViewController.h"
#import "MyViewController.h"
@interface ViewController ()
{
UILabel *label;
}
@property(nonatomic,strong)UIButton *testBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100,100,60,30)];
label = [[UILabel alloc]initWithFrame:CGRectMake(200, 100,100,30)];
label.backgroundColor = [UIColor lightGrayColor];
label.text = @"hello world";
button.backgroundColor = [UIColor redColor];
[button setTitle:@"hello" forState:UIControlStateNormal];
[button addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.view addSubview:label];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)Click:(UIButton *)sender
{
MyViewController *myView = [[MyViewController alloc]init];
myView.view.backgroundColor = [UIColor lightGrayColor];
myView.myBlockargs = ^(NSString * str)
{
label.text = str;
};
[self.navigationController pushViewController:myView animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
第二个页面:
.h文件
#import <UIKit/UIKit.h>
typedef void(^myBlock)(NSString *str);
@interface MyViewController : UIViewController
@property (nonatomic,copy)myBlock myBlockargs;
@end
.m文件
#import "MyViewController.h"
@interface MyViewController ()
{
UILabel *label;
}
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 60,30)];
button.backgroundColor = [UIColor redColor];
[button setTitle:@"world" forState:UIControlStateNormal];
[button addTarget:self action:@selector(secondView:) forControlEvents:UIControlEventTouchUpInside];
label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200,60,30)];
label.backgroundColor = [UIColor redColor];
label.text = @"second";
[self.view addSubview:button];
[self.view addSubview:label];
}
-(void)secondView:(UIButton *)sender
{
label.text = @"Second View Value";
//[self returnText:label.text];
self.myBlockargs(label.text);
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
AppDelegate.m文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *view = [[ViewController alloc]init];
UINavigationController *nva = [[UINavigationController alloc]initWithRootViewController:view];
self.window.rootViewController = nva;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
运行结果: