【问题标题】:UIActionSheet block view controller deallocUIActionSheet 块视图控制器 dealloc
【发布时间】:2015-01-29 15:31:14
【问题描述】:

当我在我的视图控制器中触摸一个按钮时,会启动这个简单的代码

- (IBAction)tapOnButton:(UIButton *)sender
{
    UIActionSheet *act = [[UIActionSheet alloc] initWithTitle:@"test" delegate:self cancelButtonTitle:@"aa" destructiveButtonTitle:@"bb" otherButtonTitles:@"cc", nil];
    [act showFromRect:CGRectMake(50, 50, 100, 100) inView:self.view animated:YES];
}

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {

    }
}

我在屏幕上看到一个 Actionsheet,点击 Actionsheet 的任何按钮,它就会关闭。

当我运行时,我的 viewcontroller 没有执行 dealloc 。 如果您不触摸按钮并且不打开操作表,dealloc 将起作用。

是一个足够大的问题。有人在吗?

我使用的是 ios 8.1.3

【问题讨论】:

  • 请将您的代码截图替换为代码的实际文本,以便人们阅读。
  • 如果您在装有 iOS 7 的设备(或模拟器)上运行代码有什么问题吗?
  • 我在 iOS 8.1 中遇到了同样的问题。也许这是 iOS 8 的错误?

标签: ios objective-c memory-leaks uiactionsheet ios8.1


【解决方案1】:

我在 iOS 8 iPad 中使用已弃用的 UIActionSheet 时遇到了同样的问题。使用 iPhone(iOS 7 和 iOS 8)和 iOS 7 iPad,问题无法重现。

目前我测试过的唯一解决方案是使用 UIAlertController 在 iOS 8 中显示操作表。如果使用 UIAlertController,视图控制器会被正确释放。

【讨论】:

    【解决方案2】:

    很可能一些引用被保留到视图控制器 - 因此它没有被释放。

    请尝试在委托方法中将委托显式设置为 nil,如下所示:

    - (IBAction)tapOnButton:(UIButton *)sender
    {
        UIActionSheet *act = [[UIActionSheet alloc] initWithTitle:@"test" delegate:self cancelButtonTitle:@"aa" destructiveButtonTitle:@"bb" otherButtonTitles:@"cc", nil];
        [act showFromRect:CGRectMake(50, 50, 100, 100) inView:self.view animated:YES];
    }
    
    -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 0)
        {
    
        }
        actionSheet.delegate = nil
    }
    

    【讨论】:

    • 代码已经写了关于ios7,应该审查所有的应用程序。但是,如果我不放置代表,当用户点击按钮时如何执行操作?我也一直看到他用 so actionsheet ,好像有些东西变了
    • 问题中的代码没有任何问题,并且该代码的任何内容都不应该导致引用循环。自 iOS 2.0 以来,人们一直在使用类似的代码,没有问题。
    • 您可以尝试在操作的委托方法中将操作表设置为 nil,以确保不会有引用循环
    • 根本没有理由为操作表创建 ivar。只需在委托方法中执行actionSheet.delegate = nil;
    • 这在 iOS 8 iPad 中对我不起作用。视图控制器仍然没有被释放。
    猜你喜欢
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    相关资源
    最近更新 更多