This commit is contained in:
wushenghua
2016-11-29 17:17:52 +08:00
parent 4147e27d97
commit a709597700
8 changed files with 284 additions and 5 deletions
@@ -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 = "<group>"; };
A930BA391DEC2D7A000A48E0 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
A930BA3B1DEC2D7A000A48E0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A930BA411DEC3234000A48E0 /* ViewController2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController2.h; sourceTree = "<group>"; };
A930BA421DEC3234000A48E0 /* ViewController2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController2.m; sourceTree = "<group>"; };
/* 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 = "<group>";
@@ -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 */,
);
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHBlockTest/ViewController.m"
timestampString = "502018955.274517"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "28"
endingLineNumber = "28"
landmarkName = "-viewDidLoad"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHBlockTest/ViewController.m"
timestampString = "502097642.465386"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "77"
endingLineNumber = "77"
landmarkName = "-pushNewAction:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHBlockTest/ViewController2.m"
timestampString = "502079171.219429"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "51"
endingLineNumber = "51"
landmarkName = "-setTitleBtnAction:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
+3
View File
@@ -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;
}
+63 -3
View File
@@ -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];
//第一个intblock的返回值,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.
@@ -0,0 +1,14 @@
//
// ViewController2.h
// ASHBlockTest
//
// Created by xmfish on 16/11/28.
// Copyright © 2016年 ash. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController
@property(nonatomic, copy) int(^getClickInfo)(NSInteger);
@end
@@ -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
+69 -2
View File
@@ -1,2 +1,69 @@
# ASHBlockTest
Block学习
#block基础用法
### 1. block定义
```
//第一个intblock的返回值,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`是为了防止使用多线程的时候出错的.
<font color = green>在另一个 VC 先定义一个属性</font> `@property(nonatomic, copy) int(^getClickInfo)(NSInteger);`
```
int (^TempBlock)(NSInteger);
TempBlock = self.getClickInfo;
if (TempBlock) {
if(TempBlock(btn.tag))
{
NSLog(@"设置成功");
}
}
```
其中设置 `TempBlock ` 也是防止多线程的时候被提前释放的问题.