1.IOS中block基本demo
A视图->B视图,B视图传值给A视图
A视图的代码片段:
- (IBAction)action2OtherView:(id)sender
{
MyView *myView = [[MyView alloc] init];
myView.func = ^(int x,int y)
{
int xy = myView.number;
NSLog(@"xy->%d",xy);
[myView removeFromSuperview];
return 0;
};
[self.view addSubview: myView];
[myView release];
}
B视图:
.h文件:
@interface MyView : UIView
@property(assign,nonatomic) int number;
@property(copy,nonatomic) int (^func)(int x,int y);
@property(retain, nonatomic) UIButton* button;
@end
.m文件:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setFrame:CGRectMake(100, 100, 300, 200)];
self.backgroundColor = [UIColor yellowColor];