iOS block循环引用
2023-09-17 本文已影响0人
小_梦
错误写法1
// ViewModel
@property (nonatomic, strong) UIViewController *controller;
// 在Viewcontroller中
@property (nonatomic, strong) VerityOtpViewModel *viewModel;
self.viewModel.controller = self;
![](https://img.haomeiwen.com/i1678863/2f373ab5ce269317.png)
正确写法1
// ViewModel
@property (nonatomic, weak) UIViewController *controller;
// 在Viewcontroller中
@property (nonatomic, strong) VerityOtpViewModel *viewModel;
self.viewModel.controller = self;;
![](https://img.haomeiwen.com/i1678863/09fd6c7e70dc3d5a.png)
错误写法2
[[_signInBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
[self.view endEditing:YES];
self.viewModel.signInType = SignInTypeDictPassword;
}];
![](https://img.haomeiwen.com/i1678863/b25a4abaf920eea1.png)
正确写法2
@weakify(self);
[[_signInBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
@strongify(self);
[self.view endEditing:YES];
self.viewModel.signInType = SignInTypeDictPassword;
}];
![](https://img.haomeiwen.com/i1678863/0fe0641e1fe6771e.png)
错误写法3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DeviceManagerCell *cell = [DeviceManagerCell cellWithTableView:tableView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.queryTrustDeviceModel = self.deviceLists[indexPath.section];
cell.deleteBlock = ^{
QueryTrustDeviceModel *subModel = self.deviceLists[indexPath.section];
[self deleteAlert:subModel.model andWithDeleteModel:subModel];
};
return cell;
}
![](https://img.haomeiwen.com/i1678863/ce84e401b3605c07.png)
正确写法3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DeviceManagerCell *cell = [DeviceManagerCell cellWithTableView:tableView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.queryTrustDeviceModel = self.deviceLists[indexPath.section];
@weakify(self);
cell.deleteBlock = ^{
@strongify(self);
QueryTrustDeviceModel *subModel = self.deviceLists[indexPath.section];
[self deleteAlert:subModel.model andWithDeleteModel:subModel];
};
return cell;
}
![](https://img.haomeiwen.com/i1678863/42ccc2ec835ef4fe.png)
错误写法4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer"];
@weakify(self);
cell.clickItemBlock = ^(CellModel * _Nonnull model) {
@strongify(self);
[self didSelectRowMehod:model tableView:tableView];
};
return cell;
}
![](https://img.haomeiwen.com/i1678863/3a6b961226f47f36.png)
正确写法4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer"];
@weakify(self);
@weakify(tableView);
cell.clickItemBlock = ^(CellModel * _Nonnull model) {
@strongify(self);
@strongify(tableView);
[self didSelectRowMehod:model tableView:tableView];
};
return cell;
}
![](https://img.haomeiwen.com/i1678863/f544bed9e85d5742.png)
错误写法5
MRPinAlertConfig *config = [[MRPinAlertConfig alloc] init];
MRPinAlert *alert = [[MRPinAlert alloc] initWithConfig:config];
self.pinAlert = alert;
[alert show];
alert.pinBlock = ^(NSString * _Nonnull pin) {
[self.pinAlert hide];
self.viewModel.pin = pin;
[self activateBiometryWithPin:pin];
};
alert.cancelBlock = ^{
kStrongSelf(self)
[self.tableView reloadData];
};
alert.forgotBlock = ^{
kStrongSelf(self)
[self.tableView reloadData];
};
![](https://img.haomeiwen.com/i1678863/2ec46e4683f6b228.png)
正确写法5
MRPinAlertConfig *config = [[MRPinAlertConfig alloc] init];
MRPinAlert *alert = [[MRPinAlert alloc] initWithConfig:config];
self.pinAlert = alert;
[alert show];
@weakify(self)
alert.pinBlock = ^(NSString * _Nonnull pin) {
@strongify(self);
[self.pinAlert hide];
self.viewModel.pin = pin;
[self activateBiometryWithPin:pin];
};
alert.cancelBlock = ^{
kStrongSelf(self)
[self.tableView reloadData];
};
alert.forgotBlock = ^{
kStrongSelf(self)
[self.tableView reloadData];
};
![](https://img.haomeiwen.com/i1678863/ac36b02a965e9922.png)
注意: 如果block未访问self,不需要写@weakify(self),kStrongSelf(self)否则会出现警告“Unused variable 'self'”