diff --git a/ASHBlockTest/ASHBlockTest.xcodeproj/project.pbxproj b/ASHBlockTest/ASHBlockTest.xcodeproj/project.pbxproj index e594582..99df586 100644 --- a/ASHBlockTest/ASHBlockTest.xcodeproj/project.pbxproj +++ b/ASHBlockTest/ASHBlockTest.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ A930BA351DEC2D7A000A48E0 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A930BA331DEC2D7A000A48E0 /* Main.storyboard */; }; A930BA371DEC2D7A000A48E0 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A930BA361DEC2D7A000A48E0 /* Assets.xcassets */; }; A930BA3A1DEC2D7A000A48E0 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A930BA381DEC2D7A000A48E0 /* LaunchScreen.storyboard */; }; + A930BA431DEC3234000A48E0 /* ViewController2.m in Sources */ = {isa = PBXBuildFile; fileRef = A930BA421DEC3234000A48E0 /* ViewController2.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -26,6 +27,8 @@ A930BA361DEC2D7A000A48E0 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; A930BA391DEC2D7A000A48E0 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; A930BA3B1DEC2D7A000A48E0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + A930BA411DEC3234000A48E0 /* ViewController2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController2.h; sourceTree = ""; }; + A930BA421DEC3234000A48E0 /* ViewController2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController2.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -67,6 +70,8 @@ A930BA381DEC2D7A000A48E0 /* LaunchScreen.storyboard */, A930BA3B1DEC2D7A000A48E0 /* Info.plist */, A930BA2A1DEC2D7A000A48E0 /* Supporting Files */, + A930BA411DEC3234000A48E0 /* ViewController2.h */, + A930BA421DEC3234000A48E0 /* ViewController2.m */, ); path = ASHBlockTest; sourceTree = ""; @@ -151,6 +156,7 @@ buildActionMask = 2147483647; files = ( A930BA321DEC2D7A000A48E0 /* ViewController.m in Sources */, + A930BA431DEC3234000A48E0 /* ViewController2.m in Sources */, A930BA2F1DEC2D7A000A48E0 /* AppDelegate.m in Sources */, A930BA2C1DEC2D7A000A48E0 /* main.m in Sources */, ); diff --git a/ASHBlockTest/ASHBlockTest.xcodeproj/project.xcworkspace/xcuserdata/xmfish.xcuserdatad/UserInterfaceState.xcuserstate b/ASHBlockTest/ASHBlockTest.xcodeproj/project.xcworkspace/xcuserdata/xmfish.xcuserdatad/UserInterfaceState.xcuserstate index 4589f28..df10a20 100644 Binary files a/ASHBlockTest/ASHBlockTest.xcodeproj/project.xcworkspace/xcuserdata/xmfish.xcuserdatad/UserInterfaceState.xcuserstate and b/ASHBlockTest/ASHBlockTest.xcodeproj/project.xcworkspace/xcuserdata/xmfish.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ASHBlockTest/ASHBlockTest.xcodeproj/xcuserdata/xmfish.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/ASHBlockTest/ASHBlockTest.xcodeproj/xcuserdata/xmfish.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..35d657f --- /dev/null +++ b/ASHBlockTest/ASHBlockTest.xcodeproj/xcuserdata/xmfish.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + diff --git a/ASHBlockTest/ASHBlockTest/AppDelegate.m b/ASHBlockTest/ASHBlockTest/AppDelegate.m index 24e3f6c..6d5e9f0 100644 --- a/ASHBlockTest/ASHBlockTest/AppDelegate.m +++ b/ASHBlockTest/ASHBlockTest/AppDelegate.m @@ -8,6 +8,7 @@ #import "AppDelegate.h" +#import "ViewController.h" @interface AppDelegate () @end @@ -17,6 +18,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. + + self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[ViewController new]]; return YES; } diff --git a/ASHBlockTest/ASHBlockTest/ViewController.m b/ASHBlockTest/ASHBlockTest/ViewController.m index 4c1280c..b6be4a4 100644 --- a/ASHBlockTest/ASHBlockTest/ViewController.m +++ b/ASHBlockTest/ASHBlockTest/ViewController.m @@ -7,9 +7,9 @@ // #import "ViewController.h" - +#import "ViewController2.h" @interface ViewController () - +@property (nonatomic, strong)UILabel* infoLabel; @end @implementation ViewController @@ -17,9 +17,69 @@ - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. + + self.title = @"首页"; + self.view.backgroundColor = [UIColor whiteColor]; + + //第一个int,block的返回值,addBlock block名称,,后面的两个int是传入的参数 + int (^addBlock)(int, int) = ^(int num1, int num2){ + return num1 + num2; + }; + + NSLog(@"相加值是:%d",addBlock(10,20)); + + //先声明,再实现 + int (^mulBlock)(int, int); + mulBlock = ^(int num1, int num2){ + return num1 * num2; + }; + + NSLog(@"相乘值是:%d",mulBlock(2,5)); + + + UIButton* pushNewBtn = [UIButton new]; + pushNewBtn.backgroundColor = [UIColor grayColor]; + [pushNewBtn setTitle:@"点击" forState:UIControlStateNormal]; + pushNewBtn.frame = CGRectMake(20, 150, 80, 50); + [pushNewBtn addTarget:self action:@selector(pushNewAction:) forControlEvents:UIControlEventTouchUpInside]; + + [self.view addSubview:pushNewBtn]; + + self.infoLabel = [UILabel new]; + self.infoLabel.backgroundColor = [UIColor whiteColor]; + self.infoLabel.textColor = [UIColor greenColor]; + self.infoLabel.frame = CGRectMake(120, 150, 180, 50); + self.infoLabel.text = @"还没有点击"; + + [self.view addSubview:self.infoLabel]; + + + __block int num = 20; + + void (^changeNum)(void) = ^(void){ + num = 30; + }; + NSLog(@"变化之前:%d",num); + changeNum(); + NSLog(@"变化之后:%d",num); } - +#pragma mark 点击进入下一页 +- (void)pushNewAction:(id)sender +{ + ViewController2* nextVC = [ViewController2 new]; + + __weak ViewController* wself = self; + + nextVC.getClickInfo = ^(NSInteger tag){ + + __strong ViewController* sself = wself; + + sself.infoLabel.text = [NSString stringWithFormat:@"点击了第%ld个按钮",tag]; + return 1; + }; + [self.navigationController pushViewController:nextVC animated:YES]; +} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. diff --git a/ASHBlockTest/ASHBlockTest/ViewController2.h b/ASHBlockTest/ASHBlockTest/ViewController2.h new file mode 100644 index 0000000..4aa7dbb --- /dev/null +++ b/ASHBlockTest/ASHBlockTest/ViewController2.h @@ -0,0 +1,14 @@ +// +// ViewController2.h +// ASHBlockTest +// +// Created by xmfish on 16/11/28. +// Copyright © 2016年 ash. All rights reserved. +// + +#import + +@interface ViewController2 : UIViewController + +@property(nonatomic, copy) int(^getClickInfo)(NSInteger); +@end diff --git a/ASHBlockTest/ASHBlockTest/ViewController2.m b/ASHBlockTest/ASHBlockTest/ViewController2.m new file mode 100644 index 0000000..79251d6 --- /dev/null +++ b/ASHBlockTest/ASHBlockTest/ViewController2.m @@ -0,0 +1,74 @@ +// +// ViewController2.m +// ASHBlockTest +// +// Created by xmfish on 16/11/28. +// Copyright © 2016年 ash. All rights reserved. +// + +#import "ViewController2.h" + +@interface ViewController2 () + +@end + +@implementation ViewController2 + +- (void)viewDidLoad { + [super viewDidLoad]; + // Do any additional setup after loading the view. + + self.title = @"第二页"; + self.view.backgroundColor = [UIColor whiteColor]; + + UIButton* btn1 = [UIButton new]; + btn1.backgroundColor = [UIColor grayColor]; + [btn1 setTitle:@"按钮1" forState:UIControlStateNormal]; + btn1.frame = CGRectMake(20, 150, 80, 50); + btn1.tag = 1; + [btn1 addTarget:self action:@selector(setTitleBtnAction:) forControlEvents:UIControlEventTouchUpInside]; + + [self.view addSubview:btn1]; + + UIButton* btn2 = [UIButton new]; + btn2.backgroundColor = [UIColor grayColor]; + [btn2 setTitle:@"按钮2" forState:UIControlStateNormal]; + btn2.frame = CGRectMake(120, 150, 80, 50); + btn2.tag = 2; + [btn2 addTarget:self action:@selector(setTitleBtnAction:) forControlEvents:UIControlEventTouchUpInside]; + + [self.view addSubview:btn2]; + +} +- (void)setTitleBtnAction:(id)sender +{ + if([sender isKindOfClass:[UIButton class]]){ + UIButton* btn = sender; + + int (^TempBlock)(NSInteger); + TempBlock = self.getClickInfo; + + if (TempBlock) { + if(TempBlock(btn.tag)) + { + NSLog(@"设置成功"); + } + } + } +} +- (void)didReceiveMemoryWarning { + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +/* +#pragma mark - Navigation + +// In a storyboard-based application, you will often want to do a little preparation before navigation +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + // Get the new view controller using [segue destinationViewController]. + // Pass the selected object to the new view controller. +} +*/ + +@end diff --git a/README.md b/README.md index 32bf6a0..3d61787 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,69 @@ -# ASHBlockTest -Block学习 +#block基础用法 + +### 1. block定义 + +``` + //第一个int,block的返回值,addBlock block名称,,后面的两个int是传入的参数 + int (^addBlock)(int, int) = ^(int num1, int num2){ + return num1 + num2; + }; + + NSLog(@"相加值是:%d",addBlock(10,20)); + + //先声明,再实现 + int (^mulBlock)(int, int); + mulBlock = ^(int num1, int num2){ + return num1 * num2; + }; + + NSLog(@"相乘值是:%d",mulBlock(2,5)); +``` +### 2. 修改局部变量 + +###需要用到 `__block` 参数修饰 + +``` + __block int num = 20; + + void (^changeNum)(void) = ^(void){ + num = 30; + }; + NSLog(@"变化之前:%d",num); + changeNum(); + NSLog(@"变化之后:%d",num); +``` +### 3. block的调用 +直接使用即可 `NSLog(@"相乘值是:%d",mulBlock(2,5));` + +### 4. 使用,可以代替delegate,这个也是我一开始喜欢它的原因 +具体可以项目内的代码,首先先设置 + +``` + __weak ViewController* wself = self; + + nextVC.getClickInfo = ^(NSInteger tag){ + + __strong ViewController* sself = wself; + + sself.infoLabel.text = [NSString stringWithFormat:@"点击了第%ld个按钮",tag]; + return 1; + }; +``` +其中 先设置`__weak`的原因是解决循环引用的问题, 内部 `__strong`是为了防止使用多线程的时候出错的. + +在另一个 VC 先定义一个属性 `@property(nonatomic, copy) int(^getClickInfo)(NSInteger);` + + +``` + int (^TempBlock)(NSInteger); + TempBlock = self.getClickInfo; + + if (TempBlock) { + if(TempBlock(btn.tag)) + { + NSLog(@"设置成功"); + } + } +``` + +其中设置 `TempBlock ` 也是防止多线程的时候被提前释放的问题. \ No newline at end of file