【问题标题】:How to pass information from appDelegate into one of the view controllers in the UINavigationcontroller如何将 appDelegate 中的信息传递到 UINavigationcontroller 中的视图控制器之一
【发布时间】:2010-10-30 22:40:46
【问题描述】:

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

现在,每当 protocolClass 从主机接收数据时,它都会在其委托(我将其设置为 appDelegate)中调用 protocolClassDidReceiveData: 方法。然后我需要更新 UINavigatorController 中一个 customViewController 中的数据。

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

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

提前致谢。

【问题讨论】:

    标签: iphone uinavigationcontroller


    【解决方案1】:

    如果我猜对了,您想在程序的某个不相关部分发生事件后更新视图。

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

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

    AppDelegate 标头:

    extern NSString* kDataReceived;
    

    AppDelegate 实现:

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

    在一些感兴趣的监听类的实现中(例如你的 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
    }
    

    【讨论】:

    • 谢谢 Nikolai,到时我会检查通知中心。最初我只是担心使用 notificationCenter 意味着会占用不必要的系统资源。
    • 我认为那将是过早的优化。如果您查看从所有视图中飞来飞去的通知数量等,我认为在从套接字接收数据后发布通知不会有任何害处。
    • 谢谢尼古拉!我正在通过 iphone 搜索事件并看到你的帖子
    【解决方案2】:

    是的,通知是一个很好的方法。当模型想要更新控制器时 [即ViewController] - 通知是一种很好的方式。就我而言,我正在尝试使用 SSDP(使用 AsyncUdpSocket)发现设备,并且我想在找到设备时更新/通知我的视图控制器。由于这是异步的,所以当接收到数据时,我使用了通知。这是我做的简单的事情:

    在 viewDidLoad 中(我尝试覆盖 init 但这对我来说效果不佳) - 我为我的 ViewController 注册了如下通知:

    *NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self
               selector:@selector(foundController:) 
                   name:@"DiscoveredController"
                 object:nil];
    

    这是我的 ViewController 中的选择器:

    // receive the notification
    - (void)foundController:(NSNotification *)note
    {
        self.controllerFoundStatus.text = @"We found a controller";
    }
    

    在我的“模型”中[不是应用程序委托 - 我创建了一个新类,用于发现设备“serviceSSDP”,我所做的只是发布如下通知:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoveredController" object:nil];
    

    就是这样。当我收到对我的 SSDP 发现的正确响应时,就会发布此通知 [特别是在:

    - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock 
         didReceiveData:(NSData *)data 
                withTag:(long)tag 
               fromHost:(NSString *)host 
                   port:(UInt16)port
    

    AsyncUdpSocket。

    【讨论】:

      猜你喜欢
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多