【问题标题】:Memory Leak using an UIAlertController in Swift在 Swift 中使用 UIAlertController 的内存泄漏
【发布时间】:2015-08-20 09:07:05
【问题描述】:

我用这个简单的代码展示了一个简单的 UIViewController

@IBAction func addNewFeed(sender: UIBarButtonItem)
{

    var alertView: UIAlertController? = UIAlertController(title: NSLocalizedString("New Feed", comment: "Titolo popup creazione feed"),
        message: NSLocalizedString("Insert the Title and the Link for the new Feed.", comment: "Messaggio creazione nuovo feed"),
        preferredStyle: UIAlertControllerStyle.Alert)


    alertView!.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "Annulla popup creazione nuovo feed"),
        style: UIAlertActionStyle.Cancel,
        handler: nil))

    presentViewController(alertView!, animated: true, completion: nil)

}

当我在界面上按下按钮时,我调用此 IBAction 并出现 UIAlertController。但是当我单击取消按钮以关闭控制器时,泄漏工具发现泄漏,如您在此图像中看到的:

我尝试在处理程序参数中放置这样的闭包:

alertView!.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "Annulla popup creazione nuovo feed"),
        style: UIAlertActionStyle.Cancel,
        handler: {[weak self] action in self!.dismissViewControllerAnimated(true, completion: nil)
        alertView = nil
        }))

但总会有泄漏。

【问题讨论】:

标签: ios swift memory-leaks instruments uialertcontroller


【解决方案1】:

UIViewController有很多陷阱要掉。

Ash Furrow 解决了这个blog post 中的许多内存问题。他尝试了弱 self 的东西,但最终决定使用一个局部变量,然后在闭包中使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多