簡體   English   中英

使用Swift中的UIAlertController進行內存泄漏

[英]Memory Leak using an UIAlertController in Swift

我使用這個簡單的代碼呈現一個簡單的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。 但是,當我點擊取消按鈕關閉控制器時,泄漏工具發現了泄漏,如下圖所示:

在此輸入圖像描述

我試過在handler參數中設置這樣的閉包:

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
        }))

但總有泄漏。

UIViewController有很多陷阱。

Ash Furrow解決了這篇博客文章中的許多內存問題。 他嘗試了弱自我,但決定使用一個局部變量,然后在閉包中使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM