Más contenido relacionado La actualidad más candente (20) Similar a Blocksの活用法 (20) Más de Hidetoshi Mori (15) Blocksの活用法4. Blocks? ?
iOS4, MacOSX 10.6から導入
無名関数、クロージャ、 Lambda
7. Block内の自動変数
int val = 1;
void (^block)(void) = ^{NSLog(@”val=%d”, val)};
val = 2;
block(); //blockの実行
> val=1
Block定義時点のローカル変数がコピーされる
8. __block指定子
__block int val = 1;
void (^block)(void) = ^{val=2)};
block();
NSLog(@”val=%d”, val);
> val=2
Blockスコープを超えるには__blockを使う
20. BlocksKitの便利メソッド
NSURLConnection#startConnectionWithRequest:successHandler:
NSURLRequest *req =
[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://d.hatena.ne.jp/h_mori/"]];
[NSURLConnection startConnectionWithRequest:req
successHandler:^(NSURLConnection *con, NSURLResponse *res, NSData *data){
NSString *html =
[[[NSString alloc] initWithData:data encoding:NSJapaneseEUCStringEncoding] autorelease];
NSLog(@"html=%@", html);
}
failureHandler:^(NSURLConnection *con, NSError *err){
NSLog(@"NSURLConnection failed : %@", [err localizedDescription]);
}];
29. UITableViewの拡張
A2DynamicUITableViewDelegateの実装
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate
respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
[realDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}
NSString *key = [NSString stringWithFormat:@”%@_%@_%@”,
kHandlerDidSelectRow, indexPath.section, indexPath.row];
BKTableViewBlock block = [self.handlers objectForKey:key];
if (block) {
((BKTableViewBlock)block)(tableView, indexPath);
}
}
30. UITableViewの拡張
UITableView (BlocksKitExtends)
- (id)initWithFrame:(CGRect)frame
style:(UITableViewStyle)style
delegate:(id)delegate
dataSource:(id)dataSource {
UITableView *tableView = [self initWithFrame:frame style:style];
tableView.delegate = self.dynamicDelegate;
((A2DynamicDelegate *)self.dynamicDelegate).realDelegate = delegate;
tableView.dataSource = self.dynamicDataSource;
((A2DynamicDelegate *)self.dynamicDataSource).realDelegate = dataSource;
return tableView;
}
31. UITableViewの拡張
UITableView (BlocksKitExtends)
typedef void (^BKTableViewBlock)(UITableView *tableView, NSIndexPath *indexPath);
- (void)setHandler:(BKTableViewBlock)block
forDidSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [NSString stringWithFormat:@”%@_%@_%@”,
kHandlerDidSelectRow, indexPath.section, indexPath.row];
if (block) {
[[self.dynamicDelegate handlers] setObject:block forKey:key];
} else {
[[self.dynamicDelegate handlers] removeObjectForKey:key];
}
[self setHandler:block forKey:key target:self.dynamicDelegate];
}
32. UITableViewの拡張
使い方
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”Cell”];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:@”Cell”] autorelease];
}
if (indexPath.section == 0) {
cell.textLabel.text = [_rows objectAtIndex:indexPath.row];
[tableView setHandler:^(UITableView *tv, NSIndexPath *ip){
//Cell選択時の処理
} forDidSelectRowAtIndexPath:indexPath];
}
return cell;
}
35. UIViewControllerの基底拡張
画面の遷移管理に使える
callback block for B
viewWillAppear:
B
execute block from B
A
callback block for C
viewWillAppear:
C
execute block from C
利点:B,CはAを意識する必要がない(疎結合になる)
Notas del editor \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n