做ppt素材网站哪个好,上海网站开发服务商,秦皇岛网站制作微商城建设,游戏程序开发Block 是 Objective-C 和 Swift 中的一种语言特性#xff0c;可以用来封装一段代码并在需要时执行。在 iOS 开发中#xff0c;Block 被广泛应用于以下场景#xff1a;
一、异步任务处理
Block 可以用于异步任务的处理#xff0c;例如网络请求、文件读写等。通过在 Block …Block 是 Objective-C 和 Swift 中的一种语言特性可以用来封装一段代码并在需要时执行。在 iOS 开发中Block 被广泛应用于以下场景
一、异步任务处理
Block 可以用于异步任务的处理例如网络请求、文件读写等。通过在 Block 中定义任务和回调可以简化异步代码的编写。
// 异步网络请求示例
[NSURLSession.sharedSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {if (error) {NSLog(Error: %, error.localizedDescription);} else {// 处理网络请求结果}
}];二、动态回调
Block 可以作为回调函数传递用于在某个操作完成后执行特定的代码。这种方式常用于处理用户交互、动画效果等。
// 动态回调示例
[UIView animateWithDuration:0.3 animations:^{// 执行动画效果
} completion:^(BOOL finished) {if (finished) {// 动画完成后执行的代码}
}];三、事件处理
Block 可以用于处理用户事件例如按钮点击事件、手势识别等。通过给控件设置 Block 回调可以简化事件处理的逻辑。
// 按钮点击事件处理示例
UIButton *button [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:Click Me forState:UIControlStateNormal];
[button addTarget:self action:selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];// Block 回调处理按钮点击事件
button.tapAction ^{NSLog(Button tapped);
};具体案例
场景UIViewContoller有个UITableView并是它的代理通过UITableView加载CellView。现在需要监听CellView中的某个按钮可以通过tag值区分并作出响应。
// 激活事件 #pragma mark - 按钮点击事件
- (IBAction)btnClickedAction:(UIButton *)sender { if (self.btnClickedBlock) { self.btnClickedBlock(sender);}
}随后在ViewController.m的适当位置- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{…代理方法中通过setter方法设置CellView的Block属性。Block写着当按钮被点击后要执行的逻辑。
// 响应事件
cell.btnClickedBlock ^(UIButton *sender) { //标记消息已读 [weakSelf requestToReadedMessageWithTag:sender.tag]; //刷新当前cell [tableView reloadRowsAtIndexPaths:[indexPath] withRowAnimation:UITableViewRowAnimationNone];
};其实即使Block不传递任何参数也可以传递事件的。但这种情况无法区分事件的激活方cell里面的哪一个按钮。即
//按钮点击Block property (nonatomic, copy) void (^btnClickedBlock)(void);// 激活事件 #pragma mark - 按钮点击事件
- (IBAction)btnClickedAction:(UIButton *)sender { if (self.btnClickedBlock) { self.btnClickedBlock();}
}// 响应事件
cell.btnClickedBlock ^{ //标记消息已读 [weakSelf requestToReadedMessageWithTag:nil]; //刷新当前cell [tableView reloadRowsAtIndexPaths:[indexPath] withRowAnimation:UITableViewRowAnimationNone];
};四、数据传递
Block 可以用于传递数据或者操作例如在界面之间传值或者执行某些操作后返回结果。
// 数据传递示例
[self presentViewController:secondViewController animated:YES completion:^{secondViewController.completionBlock ^(BOOL success) {if (success) {NSLog(Data transfer successful);}};
}];具体案例
场景上面的响应事件其实也是传递数据只是它传递的对象是UIButton。如下所示SubTableView是VC的一个属性和子视图。
4.1 传递数值
SubTableView.hproperty (strong, nonatomic) void (^handleDidSelectedItem)(int indexPath);SubTableView.m- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{[tableView deselectRowAtIndexPath:indexPath animated:YES];_handleDidSelectedItem ? _handleDidSelectedItem(indexPath) : NULL;
}VC.m[_subView setHandleDidSelectedItem:^(int indexPath) {[weakself handleLabelDidSearchTableSelectedItem:indexPath];
}];- (void)handleLabelDidSearchTableSelectedItem:(int )indexPath { if (indexPath0) {[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:telprompt:%, self.searchNullView.telLabel.text]]];}else if (indexPath1){[self.navigationController popViewControllerAnimated:YES];}
}4.2 传递对象
例如HYBNetworking网络框架中请求成功时传递接口返回数据对象的Block:
[HYBNetworking postWithUrl:kSearchProblem refreshCache:NO params:params success:^(id response) { typeof(weakSelf) strongSelf weakSelf; //[KVNProgress dismiss]; NSString *stringData [response mj_JSONString];stringData [DES3Util decrypt:stringData]; NSLog(stirngData: %, stringData);...
}总的来说iOS 中的 Block 是一种非常灵活和强大的工具可以简化代码逻辑、实现回调机制、处理异步任务等。合理地使用 Block 可以提高代码的可读性和可维护性同时也能更好地适应 iOS 开发中的各种需求。