Iphone 如何将信息从appDelegate传递到UINavigationcontroller中的一个视图控制器
在我正在开发的iphone应用程序中,我使用一个自定义类来管理与主机的网络通信。名为protocolClass的类是appDelegate中的ivar和ApplicationIDFinishLaunching:方法中的alloc+init 现在,每当protocolClass从主机接收数据时,它都在其委托中调用protocolClassDidReceiveData:方法(我将其设置为appDelegate)。然后,我需要更新UINavigatorController中某个CustomViewController中的数据 我是否应该在appDelegate中添加对需要更新的customViewController的引用?还是有其他更有效的方法 如果我保留对customViewcontroller的引用,内存使用会产生什么后果Iphone 如何将信息从appDelegate传递到UINavigationcontroller中的一个视图控制器,iphone,uinavigationcontroller,Iphone,Uinavigationcontroller,在我正在开发的iphone应用程序中,我使用一个自定义类来管理与主机的网络通信。名为protocolClass的类是appDelegate中的ivar和ApplicationIDFinishLaunching:方法中的alloc+init 现在,每当protocolClass从主机接收数据时,它都在其委托中调用protocolClassDidReceiveData:方法(我将其设置为appDelegate)。然后,我需要更新UINavigatorController中某个CustomViewCo
提前谢谢 如果我没记错的话,您希望在程序的某个不相关部分发生事件后更新视图 为了减少代码中的依赖项数量,我建议使用NSNotification而不是更紧密耦合的实例变量。通知是一个Cocoa概念,它允许代码的一部分发出类似事件的消息,任何数量的侦听器都可以注册该消息 在您的情况下,它将如下所示: AppDelegate标头:
extern NSString* kDataReceived;
AppDelegate实现:
NSString* kDataReceived = @"DataReceived";
- (void)protocolClassDidReceiveData:(NSData*)data {
[[NSNotificationCenter defaultCenter] postNotificationName:kDataReceived
object:self
userInfo:data];
}
在某些感兴趣的侦听器类(例如UIViewController)的实现中:
是的,通知是一种很好的方式。当模型想要更新控制器[即ViewController]时,通知是一种很好的方法。在我的例子中,我试图使用SSDP(使用AsyncUdpSocket)发现设备,并且我希望在找到设备时更新/通知我的视图控制器。因为这是异步的,所以在接收数据时,我使用了通知。下面是我做的一件简单的事情: 在viewDidLoad中(我尝试重写init,但对我来说效果不好)——我注册了ViewController以获得通知,如下所示:
*NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(foundController:)
name:@"DiscoveredController"
object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoveredController" object:nil];
以下是我的ViewController中的选择器:
// 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
}
// receive the notification
- (void)foundController:(NSNotification *)note
{
self.controllerFoundStatus.text = @"We found a controller";
}
在我的“模型”[不是应用程序委托-我创建了一个新类,用于发现设备“serviceSSDP”,我所做的只是发布通知,如下所示:
*NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(foundController:)
name:@"DiscoveredController"
object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoveredController" object:nil];
就是这样。当我得到对我的SSDP发现的正确响应时,就会发布此通知[特别是在:
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock
didReceiveData:(NSData *)data
withTag:(long)tag
fromHost:(NSString *)host
port:(UInt16)port
AsyncUdpSocket。谢谢Nikolai,我会检查notificationCenter。起初我只是担心使用notificationCenter意味着使用不必要的系统资源。我认为这是过早的优化。如果你看看有多少通知从所有视图中飞来飞去,我认为不会在收到来自套接字的数据后发布通知有什么坏处吗?谢谢Nikolai!我在用谷歌搜索iphone中传递的事件,看到了你的帖子