Xamarin NSNotificatioCenter:我怎样才能让 NSObject 被传递? [英] Xamarin NSNotificatioCenter: How can I get the NSObject being passed?

查看:18
本文介绍了Xamarin NSNotificatioCenter:我怎样才能让 NSObject 被传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 NSNotificationCenter 从我的应用程序向另一个应用程序的视图中发布通知.所以在我的目标类中,我按如下方式创建了我的观察者:

I am trying to post a notification in a view from my app to another one using NSNotificationCenter. So in my destination class I create my observer as follows:

NSNotificationCenter.DefaultCenter.AddObserver ("ChangeLeftSide", delegate {ChangeLeftSide(null);});

我有我的方法:

public void ChangeLeftSide (UIViewController vc)
{
    Console.WriteLine ("Change left side is being called");
}

现在从另一个 UIViewController 我发布如下通知:

Now from another UIViewController I am posting a notification as follows:

NSNotificationCenter.DefaultCenter.PostNotificationName("ChangeLeftSide", this);

如何访问在目标类的帖子通知中传递的视图控制器?在 iOS 中,它非常简单,但我似乎无法在单点触控(Xamarin)中找到自己的方式......

How can I access the view controller that is being passed in my post notification in my destination class? In iOS it is very straight forward but I cannot seem to find my way in monotouch (Xamarin)...

推荐答案

我找到了答案,这里是我在问题中发布的代码需要进行的更改:

I found the answer, here are the changes that need to be made on the code I posted in the question:

public void ChangeLeftSide (NSNotification notification)
{
    Console.WriteLine ("Change left side is being called");
    NSObject myObject = notification.Object;
    // here you can do whatever operation you need to do on the object
}

观察者被创建:

NSNotificationCenter.DefaultCenter.AddObserver ("ChangeLeftSide", ChangeLeftSide);

现在您可以转换或键入检查 NSObject 并对其进行任何操作!大功告成!

Now you can cast or type check the NSObject and do anything with it! Done!

这篇关于Xamarin NSNotificatioCenter:我怎样才能让 NSObject 被传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆