xcode 如何在另一个视图控制器中调用 NSNotificationCenter 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8292962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:34:31  来源:igfitidea点击:

How to call a NSNotificationCenter function in another view controller?

iphoneobjective-ciosxcodenotifications

提问by pixelbitlabs

Possible Duplicate:
Can I watch an NSNotification from another class?

可能的重复:
我可以从另一个类观看 NSNotification 吗?

I am currently using this code below in my appDelegate.m file:

我目前在我的 appDelegate.m 文件中使用以下代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnteredBackground:) 
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];

}

However, I want it to call a selector in my viewController.m file. How can I do this?

但是,我希望它在我的 viewController.m 文件中调用一个选择器。我怎样才能做到这一点?

Thanks!

谢谢!

回答by Alan Zeino

Generally, you register the notification first, in your viewController.m initmethod (or somewhere else appropriate):

通常,您首先在 viewController.minit方法(或其他适当的地方)中注册通知:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(someMethod:) 
                                             name: @"NotificationNameHere"
                                           object: nil];

In your App Delegate, fire the notification:

在您的 App Delegate 中,触发通知:

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    [[NSNotificationCenter defaultCenter] postNotification: @"NotificationNameHere"];

}

回答by Kevin

in your viewController.m

在你的 viewController.m

- (void)viewDidLoad
{

   [super viewDidLoad];

   [[NSNotificationCenter defaultCenter] addObserver: self
                                     selector: @selector(handleEnteredBackground:) 
                                         name: UIApplicationDidEnterBackgroundNotification
                                       object: nil];
}

回答by krafter

just add this:

只需添加这个:

[[NSNotificationCenter defaultCenter] addObserver: self
                                     selector: @selector(handleEnteredBackground:) 
                                         name: UIApplicationDidEnterBackgroundNotification
                                       object: nil];

in your viewController.m viewDidLoad method. (don't forget to create handleEnteredBackground: method)

在您的 viewController.m viewDidLoad 方法中。(不要忘记创建 handleEnteredBackground: 方法)

回答by jbat100

You don't want to register your app delegate for entering background notifications, you already get callbacks for that. It makes even less sense to register your app delegate for these notifications in that callback. You can just, as other posters have suggested add your viewController object as an observer on init (and don't forget to remove yourself on dealloc or you will get bad accesses).

你不想注册你的应用程序委托来输入后台通知,你已经得到了回调。在该回调中为这些通知注册您的应用程序委托更没有意义。您可以像其他海报建议的那样将您的 viewController 对象添加为 init 上的观察者(不要忘记在 dealloc 上删除自己,否则您将获得错误的访问权限)。

回答by daveoncode

If the problem is to access to the delegate from a viewController, you can do:

如果问题是从 viewController 访问委托,您可以执行以下操作:

MYAppDelegate *delegate = (MYAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate selectorName];

...otherwise clarify your question :P

...否则澄清你的问题:P