【问题标题】:Send a notification from AppDelegate to ViewController从 AppDelegate 向 ViewController 发送通知
【发布时间】:2013-05-09 09:01:49
【问题描述】:

需要你的帮助。我将这些委托方法实现到 AppDelegate.m:

    -(BOOL)application:(UIApplication *)application
           openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication
        annotation:(id)annotation {
    if (url != nil && [url isFileURL]) {
        //Valid URL, send a message to the view controller with the url
    }
    else {
        //No valid url
    }
    return YES;

但是现在,我需要的不是 AppDelegate 中的 URL,而是我的 ViewController 中的 URL。我如何向他们“发送” url 或如何将这些委托方法实现到 ViewController?

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    你可以使用NSNotificationCenter,如下图:

    首先在您的应用代理中发布通知:

    NSDictionary *_dictionary=[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%d",newIndex],nil] forKeys:[NSArray arrayWithObjects:SELECTED_INDEX,nil]];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:SELECT_INDEX_NOTIFICATION object:nil userInfo:_dictionary];
    

    然后注册你想要观察这个通知的ViewController

    /***** To register and unregister for notification on recieving messages *****/
    - (void)registerForNotifications
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(yourCustomMethod:)
                                                     name:SELECT_INDEX_NOTIFICATION object:nil];
    }
    
    /*** Your custom method called on notification ***/
    -(void)yourCustomMethod:(NSNotification*)_notification
    {
        [[self navigationController] popToRootViewControllerAnimated:YES];
        NSString *selectedIndex=[[_notification userInfo] objectForKey:SELECTED_INDEX];
        NSLog(@"selectedIndex  : %@",selectedIndex);
    
    }
    

    在 ViewDidLoad 中调用这个方法:

    - (void)viewDidLoad
    {
    
        [self registerForNotifications];
    }
    

    然后在 UnLoad 时调用这个方法移除这个观察者:

    -(void)unregisterForNotifications
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:SELECT_INDEX_NOTIFICATION object:nil];
    }
    
    
    -(void)viewDidUnload
    {
        [self unregisterForNotifications];
    }
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      您可以像这样发布本地通知,接收者将使用通知名称来订阅。

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

      并在您的 viewController 中订阅通知。

      [[NSNotificationCenter defaultCenter] 
          addObserver:self 
          selector:@selector(getData:) 
          notificationName:@"Data" 
          object:nil];
      
      - getData:(NSNotification *)notification {
          NSString *tappedIndex = [[_notification userInfo] objectForKey:@"KEY"];
      }
      

      更多关于NSNotificationCenter Can go with this link

      【讨论】:

      • 嗨,我必须把这段代码放在哪里:[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getData:) notificationName:@"Data" object:yourdata];
      • 你想在哪里检查通知数据,就像你可以在 viewDidLoad 中调用它一样
      • 我们如何在 viewController 中获取 yourData 作为对象?
      • 只是把nil作为ViewController上的对象引用,数据附加到实际通知中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多