ViewController在Xamarin导航堆栈弹出后没有被释放.iOS

问题描述:

我想向人们保证,当使用按钮从堆栈弹出控制器时,我已经通过互联网解释了所有内容,解释了内存泄漏,而我意识到UIButton在引发Lambdas事件的同时创建的强大引用。ViewController在Xamarin导航堆栈弹出后没有被释放.iOS

我曾尝试过所有似乎都没有为我工作。

问题陈述

我有一个UICollectionViewController为根视图控制器和我所编程方式创建并添加为子视图在它上面的浮动按钮。

这些按钮将视图控制器推入堆栈。

这里是我推控制器的方法。

private void HandleClick(object sender, EventArgs e) { 

    var button = sender as UIButton; 
    var board = UIStoryboard.FromName("Main", NSBundle.MainBundle); 
    switch (button.Tag) { 

     case 3: { 
       var vc = board.InstantiateViewController("BibleViewController"); 
       this.NavigationController.PushViewController(vc, true); 
       vc = null; 

       break; 

      } 
     case 5: { 
       var vc = board.InstantiateViewController("RecordingViewController"); 
       this.NavigationController.PushViewController(vc, true); 
       vc = null; 

       break; 
      } 
     case 7: { 
       var vc = board.InstantiateViewController("CameraFbController"); 
       this.NavigationController.PushViewController(vc, true); 
       vc = null; 

       break; 
      } 
     case 6: { 
       var vc = board.InstantiateViewController("NewEmojiController"); 
       this.NavigationController.PushViewController(vc, true); 
       vc = null; 

       break; 
      } 
     case 4: { 
       var vc = board.InstantiateViewController("WriteNShareController"); 
       this.NavigationController.PushViewController(vc, true); 
       vc = null; 
       break; 
      } 
     default : { 
       break; 
      } 



    } 
} 

假设我推BibleViewController(案例3 :)

请找到代码此控制器

public partial class BibleHomeController : UIViewController 
{ 
    IList<string> items; 
    IList<string> item1; 

    public BibleHomeController() : base("BibleHomeController", null) 
    { 
    } 
    public BibleHomeController(IntPtr handle) : base (handle) 
    { 
    } 
    ~BibleHomeController() { 

     Console.WriteLine("it was called "); 

    } 
    protected override void Dispose(bool disposing) 
    { 
     base.Dispose(disposing); 
    } 
    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     LoadJson(); 

     tableView.DataSource = new BTableViewDataSource(items); 
     tableView.Delegate = new TableDelegate(items,this); 
     tableView.RegisterNibForCellReuse(UINib.FromName("BookCell",NSBundle.MainBundle),BookCell.Key); 




    } 
    public override void ViewWillAppear(bool animated) 
    { 
     base.ViewWillAppear(animated); 
     backBtn.TouchUpInside += HandleBackClick; 
     nwBtn.TouchUpInside += newBtn; 
     oldBtn.TouchUpInside += oldBtnHanle; 
    } 
    private void HandleBackClick(object sender, EventArgs e) 
    { 

     this.NavigationController.PopViewController(true); 
    } 
    public override void ViewWillDisappear(bool animated) 
    { 
     base.ViewWillDisappear(animated); 
     backBtn.TouchUpInside -= HandleBackClick; 
     nwBtn.TouchUpInside -= newBtn; 
     oldBtn.TouchUpInside -= oldBtnHanle; 
        backBtn = null; 
     nwBtn = null; 
     oldBtn = null; 
     tableView = null; 

    } 
    private void newBtn(object sender, EventArgs e) 
    { 

     tableView.DataSource = new BTableViewDataSource(item1); 
     tableView.Delegate = new TableDelegate(item1,this); 
     tableView.ReloadData(); 
    } 
    private void oldBtnHanle(object sender, EventArgs e) 
    { 

     tableView.DataSource = new BTableViewDataSource(items); 
     tableView.Delegate = new TableDelegate(items,this); 
     tableView.ReloadData(); 
    } 
    public override void DidReceiveMemoryWarning() 
    { 
     base.DidReceiveMemoryWarning(); 
     // Release any cached data, images, etc that aren't in use. 
    } 
    private void LoadJson() { 
     using (StreamReader r = new StreamReader("BibleSection/BibleBooks/Books.json")) { 
      string json = r.ReadToEnd(); 
      items = JsonConvert.DeserializeObject<List<string>>(json); 
     } 
     using (StreamReader r = new StreamReader("BibleSection/BibleBooks/NewBook.json")) 
     { 
      string json = r.ReadToEnd(); 
      item1 = JsonConvert.DeserializeObject<List<string>>(json); 
     } 
    } 

} 
public class BTableViewDataSource : UITableViewDataSource 
{ 
    IList<string> data; 

    public BTableViewDataSource(IList<string> list) { 

     data = list; 
    } 
    ~BTableViewDataSource() { 
     Console.WriteLine("it was called "); 
    } 
    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
    { 
     // if cell is not available in reuse pool, iOS will create one automatically 
     // no need to do null check and create cell manually 
     var cell = (BookCell)tableView.DequeueReusableCell("BookCell", indexPath) as BookCell; 
     cell.PopulateCell(data[indexPath.Row], ""); 
     cell.SetNeedsLayout(); 

     //cell.SeparatorInset = UIEdgeInsets.Zero; 
      return cell; 

    } 

    public override nint RowsInSection(UITableView tableView, nint section) 
    { 

     return data.Count; 

    } 


} 
public class TableDelegate : UITableViewDelegate { 
    IList<string> data; 
    BibleHomeController owner; 
    public TableDelegate(IList<string> list, BibleHomeController reference) 
    { 
     owner = reference; 
     data = list; 
    } 
    ~TableDelegate() 
    { 
    Console.WriteLine("it was called "); 
    } 
    public override void RowSelected(UITableView tableView, NSIndexPath indexPath) 
    { 
     //base.RowSelected(tableView, indexPath); 
     var board = UIStoryboard.FromName("Main", NSBundle.MainBundle); 
     var vc = (BibleChapterCollectionview)board.InstantiateViewController("BibleChapterCollectionview") as BibleChapterCollectionview; 
     vc.itemName = data[indexPath.Row]; 
     owner.NavigationController.PushViewController(vc, true); 

    } 

} 

我的问题是,当我在BibleViewController弹出控制器的 析构没有任何类被调用,因此不会调用dispose,因此控制器内存不会被释放。

所以每次我推动和流行我添加一些内存堆。

我想指出,我从viewDidDisappear方法中的按钮中分离所有事件处理程序。

当我弹出控制器时,请你帮我解释如何释放资源。

编辑: 我曾想过问题是与tableview.delegate和table.datasource行。 如果我评论他们的问题解决了。

我应该使用weakDelegate吗?

修改这部分代码适用于我。

private void HandleBackClick(object sender, EventArgs e) 
    { 
      tableView.Delegate = null; 
      tableView.DataSource = null; 
      tableView.Source = null; 
     this.NavigationController.PopViewController(true); 
    } 

//下面的修改是不相关的问题的陈述,但需要为按钮不需要被清零

public override void ViewWillDisappear(bool animated) 
{ 
    base.ViewWillDisappear(animated); 
    backBtn.TouchUpInside -= HandleBackClick; 
    nwBtn.TouchUpInside -= newBtn; 
    oldBtn.TouchUpInside -= oldBtnHanle; 


}