本文介绍了如何将信息从appDelegate传递到UINavigationcontroller中的视图控制器之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在使用的iPhone应用程序中,我使用自定义类来管理与主机的网络通信.名为protocolClass的类在appDelegate中是一个ivar,在applicationDidFinishLaunching:方法中是alloc + init.

In the iphone app that I'm working on I use a custom class to manage network communication with the host. The class called protocolClass is an ivar in the appDelegate and alloc + init in the applicationDidFinishLaunching: method.

现在,无论何时protocolClass从主机接收数据,它都会在其委托中调用protocolClassDidReceiveData:方法(我将其设置为appDelegate).然后,我需要在UINavigatorController中的customViewControllers中更新数据.

Now whenever the protocolClass receive data from the host, it calls protocolClassDidReceiveData: method in its delegate (which I set as the appDelegate). I need then to update the data in one of the customViewControllers in the UINavigatorController.

我应该只添加对我需要在appDelegate中更新的customViewController的引用吗?还是还有其他更有效的方法?

Should I just add a reference to the customViewController I need to update in the appDelegate? or is there some other more efficient method?

如果我要保留对customViewcontroller的引用,那么内存使用的后果是什么?

If I were to keep a reference to the customViewcontroller, what are the memory usage ramifications?

先谢谢了.

推荐答案

如果我说对了,您想在程序的某些无关部分中发生事件后更新视图.

If I get you right, you want to update a view after an event occurs in some unrelated part of your program.

要减少代码中的依赖项数量,建议您使用NSNotification而不是更紧密耦合的实例变量.通知是一种Cocoa概念,它使您的代码的一部分可以发出类似事件的消息,任何数量的侦听器都可以注册.

To reduce the number of dependencies in your code, I'd recommend to use an NSNotification instead of the more tightly coupled instance variable. Notifications are a Cocoa concept, which allows one part of your code to emit an event-like message that any number of listeners can register for.

在您的情况下,它看起来像这样:

In your case it would look like this:

AppDelegate标头:

AppDelegate header:

extern NSString* kDataReceived;

AppDelegate实现:

AppDelegate implementation:

NSString* kDataReceived = @"DataReceived";

- (void)protocolClassDidReceiveData:(NSData*)data {
    [[NSNotificationCenter defaultCenter] postNotificationName:kDataReceived
                                                        object:self
                                                      userInfo:data];
}

在实现一些感兴趣的侦听器类(例如您的UIViewController)时:

in the implementation of some interested listener class (e.g. your UIViewController):

// register for the notification somewhere
- (id)init
{
    self = [super init];
    if (self != nil) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(dataReceivedNotification:)
                                                     name:kDataReceived
                                                   object:nil];
    }
}

// unregister
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

// receive the notification
- (void)dataReceivedNotification:(NSNotification*)notification
{
    NSData* data = [notification userInfo];
    // do something with data
}

这篇关于如何将信息从appDelegate传递到UINavigationcontroller中的视图控制器之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:02