Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将QMUIAlertController作为全局属性使用时QMUIAlertAction回调只能执行一次 #427

Closed
codingiran opened this issue Nov 10, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@codingiran
Copy link

commented Nov 10, 2018

请填写运行环境

  • 设备:iPhone / 模拟器
  • 系统:iOS 11.1
  • Xcode 版本:10.1
  • QMUI iOS 版本:2.9.0

请描述具体问题

将QMUIAlertController设置为强引用全局属性

@property(nonatomic, strong) QMUIAlertController *alertController;

懒加载之:

- (QMUIAlertController *)alertController
{
    if (!_alertController) {
        _alertController = [QMUIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
        QMUIAlertAction *add = [QMUIAlertAction actionWithTitle:@"添加或移除签名" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
            WESignatureViewController *signatureVc = [[WESignatureViewController alloc] init];
            signatureVc.supportedOrientationMask = UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
            WENavigationController *navigation = [[WENavigationController alloc] initWithRootViewController:signatureVc];
            [[QMUIHelper visibleViewController] presentViewController:navigation animated:YES completion:NULL];

            // 目前我的解决方案为:在回调执行完后将_alertController置空,以确保下次弹出时拿到新的_alertController
            self->_alertController = nil;

        }];
        QMUIAlertAction *cancel = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
            
        }];
        [_alertController addAction:add];
        [_alertController addAction:cancel];

    }
    return _alertController;
}

如上操作,QMUIAlertAction *add的handler回调在self.alertController第一次show的时候正常进入,但hide之后再次show时,QMUIAlertAction *add的回调再也无法进入

查看源码(QMUIAlertController.m line:1160):

#pragma mark - <QMUIAlertActionDelegate>

- (void)didClickAlertAction:(QMUIAlertAction *)alertAction {
    [self hideWithAnimated:YES completion:^{
        if (alertAction.handler) {
            alertAction.handler(self, alertAction);
            **alertAction.handler = nil;**
        }
    }];
}

发现alertAction.handler = nil;,设nil应该是为了避免循环引用吧,不知道是否有更好的解决方案

目前我的解决方案为:在回调执行完后将_alertController置空,以确保下次弹出时拿到新的_alertController
self->_alertController = nil;(如上面回调中的备注)

Mark: 我在QMUI微信群里,如果我表达的不清楚可以@codingiran

@codingiran codingiran changed the title 将QMUIAlertController作为全局属性使用时QMUIAlertAction回调只能执行 将QMUIAlertController作为全局属性使用时QMUIAlertAction回调只能执行一次 Nov 10, 2018

@zhoon

This comment has been minimized.

Copy link
Collaborator

commented Nov 13, 2018

@codingiran 我们已经修改了,去掉 = nil 这个操作,外面做好 weak 就好了,不会循环引用。下个版本会带上这个修改。

@MoLice

This comment has been minimized.

Copy link
Collaborator

commented Nov 13, 2018

2.9.1 已发布并修复该问题,请更新版本后尝试。

@MoLice MoLice closed this Nov 13, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.