gpt4 book ai didi

c# - 键值观察不会在 UIViewController 子类上触发?

转载 作者:行者123 更新时间:2023-11-30 16:49:20 25 4
gpt4 key购买 nike

我试图在 UIViewController 的标题更改时收到通知。

我尝试将观察者添加到 UIViewController 子类的标题中,但它从未被触发。这有什么奇怪的,它适用于普通的 UIViewController。我做错了什么吗?

这是解释我的问题的代码示例 (Xamarin.iOS C#):

using System;

using UIKit;
using System.Collections.Generic;

namespace ObserverTests
{
public partial class ViewController : UIViewController
{
List<UIViewController> viewControllers = new List<UIViewController>();

public override void ViewDidLoad()
{
UIViewController controller1 = new UIViewController() { Title = "Controller1" };
UIViewController controller2 = new Test() { Title = "Controller2" };

this.viewControllers.Add(controller1);
this.viewControllers.Add(controller2);

foreach(UIViewController viewController in viewControllers)
{
viewController.AddObserver("title", Foundation.NSKeyValueObservingOptions.New, (changes) =>
{
Console.WriteLine(viewController.Title);
Console.WriteLine("Title Changed!");
});
}

controller1.Title = "TitleChanged1"; // Works
controller2.Title = "TitleChanged2"; // Fails
}

private class Test : UIViewController
{
}
}
}

最佳答案

在 Xamarin 中,最好的方法可能是使用继承并添加这样的功能。为此,您从 UIViewController 派生

public class UIObserveTitleChangedViewController : UIViewController
{
public event TitleChangedEvent TitleChanged;
public override string Title
{
get
{
return base.Title;
}

set
{
var oldTitle = base.Title;
if (oldTitle == value)
return;
base.Title = value;
OnTitleChanged(new TitleChangedEventArgs(value, oldTitle));
}
}

protected virtual void OnTitleChanged(TitleChangedEventArgs args)
{
TitleChanged?.Invoke(this, args);
}

#region ctor
public UIObserveTitleChangedViewController() { }
public UIObserveTitleChangedViewController(NSCoder coder) : base(coder) { }
protected UIObserveTitleChangedViewController(NSObjectFlag t) : base(t) { }
protected internal UIObserveTitleChangedViewController(IntPtr handle) : base(handle) { }
public UIObserveTitleChangedViewController(string nibName, NSBundle bundle) : base(nibName, bundle) { }
#endregion
}

并实现缺失的事件类型

public delegate void TitleChangedEvent(object sender, TitleChangedEventArgs args);

public class TitleChangedEventArgs : EventArgs
{
public string NewTitle { get; set; }
public string OldTitle { get; set; }

public TitleChangedEventArgs(string newTitle, string oldTitle)
{
NewTitle = newTitle;
OldTitle = oldTitle;
}
}

然后您可以订阅此事件并获得更改通知

public partial class ViewController : UIObserveTitleChangedViewController
{
public ViewController(IntPtr handle) : base(handle)
{
this.TitleChanged += ViewController_TitleChanged; // Subscribe to TitleChanged
}

public override void ViewDidLoad()
{
base.ViewDidLoad();
Title = "Some title"; // triggers TitleChanged
Title = "Another new title"; // triggers TitleChanged
}

private void ViewController_TitleChanged(object sender, TitleChangedEventArgs args)
{
Debug.WriteLine("Title changed from {0} to {1}", args.OldTitle, args.NewTitle);
}
}

关于c# - 键值观察不会在 UIViewController 子类上触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36849469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com