How do I pass data from a tab bar controller to one of its tabs?

后端 未结 6 787
滥情空心
滥情空心 2021-02-02 09:21

I have a UITabBarController set up in storyboard. I want to pass a dictionary of data from the tab bar controller for use in the appropriate child tab, which is a standard UIVie

6条回答
  •  长情又很酷
    2021-02-02 10:08

    Your best bet is to use notifications.

    In your tab bar, you would do this:

    NSDictionary *myDictionary; // Populate this with your data.
    
    // Fire the notification along with your NSDictionary object.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Some_Notification_Name" 
                                                        object:myDictionary];        
    

    Then in your child tab ViewController, you would "listen" for that notification.

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(handleNotification:) 
                                                 name:@"Some_Notification_Name" 
                                               object:nil];
    
    - (void)handleNotification:(id)object {
      // Use your NSDictionary object here.
    }
    

提交回复
热议问题