【问题标题】:Where or when to call RemoveObserver何时何地调用 RemoveObserver
【发布时间】:2016-04-25 14:50:51
【问题描述】:

我有一个 UITextView 子类,我在其中添加了一个 NSNotificationCenter 观察者。但是我在哪里再次删除观察者?

我的代码:

_textDidChangeNotification = UITextView.Notifications.ObserveTextDidChange(TextDidChange);

在 Objective C 中,我会在 dealloc 方法中执行此操作,但我不确定在 C# 中的何处执行相同操作

据我了解我应该调用的文档

_textDidChangeNotification.Dispose()

我已经尝试过

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        if (disposing)
        {
            _textDidChangeNotification.Dispose();
        }
    }

但它永远不会被调用。

完整的课程,根据要求:

public class PlaceholderTextView : UITextView
{
    public string Placeholder 
    { 
        get { return PlaceholderLabel.Text; }
        set
        { 
            PlaceholderLabel.Text = value; 
            PlaceholderLabel.SizeToFit();
        }
    }

    protected UILabel PlaceholderLabel { get; set; }

    protected NSObject _textDidChangeNotification;

    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            base.Text = value;
            AdjustPlaceholderHidden();
        }
    }

    public PlaceholderTextView() 
    {
        SetupLayout();

        _textDidChangeNotification
        = UITextView.Notifications.ObserveTextDidChange(TextDidChange);
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        _textDidChangeNotification.Dispose();
    }

    protected void SetupLayout()
    {
        PlaceholderLabel = new UILabel(new CGRect(0, 9, 0, 0));
        PlaceholderLabel.TextColor = UIColor.FromWhiteAlpha(0.702f, 1f);

        AddSubview(PlaceholderLabel);
    }

    protected void AdjustPlaceholderHidden()
    {
        if (Text.Length > 0)
        {
            PlaceholderLabel.Hidden = true;
        }
        else
        {
            PlaceholderLabel.Hidden = false;
        }
    }

    protected void TextDidChange(object sender, Foundation.NSNotificationEventArgs args)
    {
        AdjustPlaceholderHidden();
    }       
}

【问题讨论】:

    标签: c# xamarin xamarin.ios nsnotificationcenter


    【解决方案1】:

    我会在ViewWillDisappear 这样做:

    public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear (animated);
    
            SubscribeMessages ();
        }
    
        public override void ViewWillDisappear(bool animated)
        {
            base.ViewWillDisappear(animated);
            UnSubscribeMessages ();
        }
    
        public void SubscribeMessages ()
        {
            _hideObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
            _showObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
        }
    
        public void UnSubscribeMessages ()
        {
            if (_hideObserver != null) NSNotificationCenter.DefaultCenter.RemoveObserver (_hideObserver);
            if (_showObserver != null) NSNotificationCenter.DefaultCenter.RemoveObserver(_showObserver);
        }
    

    ViewDidDisappear 就像 Xamarin 示例代码中的 here

    更新 我现在明白你的意思了,我怀疑有什么东西阻止了自定义视图被垃圾收集。你有没有看过这个blog post 它可能会有所帮助。

    此外,从这个sample code 看来,您正在正确调用 dispose,但它们使 ViewDidUnload here 上的自定义视图无效:

    【讨论】:

    • 有问题的类是 UIView (UITextView) 的子类,所以没有 ViewWillDisappear 或 ViewDidDisappear 或类似的。
    • 你将 UITextView 添加到什么?你可以在你添加视图的任何地方调用 UITextView 上的 dispose 吗?
    • 当然我可以从顶部的 UIViewController 一路手动完成。但这不会是一个正常的 UIView。如果是 Objective C,我可以在 dealloc 方法中做到这一点
    • 能否贴出完整的代码让我们看看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多