iOS block循環(huán)引用

錯誤寫法1

// ViewModel
@property (nonatomic, strong) UIViewController *controller;

// 在Viewcontroller中
@property (nonatomic, strong) VerityOtpViewModel *viewModel;

 self.viewModel.controller = self;
錯誤1.png

正確寫法1

// ViewModel
@property (nonatomic, weak) UIViewController *controller;

// 在Viewcontroller中
@property (nonatomic, strong) VerityOtpViewModel *viewModel;

 self.viewModel.controller = self;;
正確1.png

錯誤寫法2

[[_signInBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    [self.view endEditing:YES];
    self.viewModel.signInType = SignInTypeDictPassword;
}];
錯誤2.png

正確寫法2

@weakify(self);
[[_signInBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    @strongify(self);
    [self.view endEditing:YES];
    self.viewModel.signInType = SignInTypeDictPassword;
}];
正確2.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;
}
錯誤3.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;
}
正確3.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;
}
錯誤4.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;
}
正確4.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];
};
錯誤5.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];
};
正確5.png

注意: 如果block未訪問self,不需要寫@weakify(self),kStrongSelf(self)否則會出現(xiàn)警告“Unused variable 'self'”

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容