Updating All UITabBarController View Controllers

I have UITabBarControllerfour tabs. In each of the view controllers presented when the tab was selected, I have a reset button. When the button is pressed, the appearance of all view controllers will change. In particular, it will change the text of some labels in different view managers.

Is there a recommended way to update all view controllers at the same time UITabBarControllerto force them to reload their views?

My current approach is to make these view controllers conform to the protocol

@protocol XYReloadableViewController
- (void)reloadContents;
@end

and then send a message -reloadContentsto all view controllers when the button is pressed:

- (IBAction)touchUpInsideResetButton {
    // ...
    NSArray *viewControllers = self.tabBarController.viewControllers;
    for (UIViewController<XYReloadableViewController> *viewController in viewControllers) {
        [viewController reloadContents];
    }
}

Then, in each of the view controllers, I would have to implement this method:

- (void)reloadContents {
    [self.tableView reloadData];
    // other updates to UI ...
}

. , ?


: , UINavigationController ? ...

+4
2

. reset, NSNotification - , reset, , . , , .

, , . BOOL needsUpdate, VC , viewWillAppear:, (, , ).

, UIViewController, . ( Apple VC) UIViewController, .

+2

ReloadViewController contrlollers .

ReloadViewController UIButton :

  • - () reloadContents;
  • - (IBAction) touchUpInsideResetButton: (ID) ;

.m :

-(void)viewDidLoad
{
  [super viewDidLoad];
  [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(reloadContents)
                                                 name:@"MyNotification"
                                               object:nil];
}

- (IBAction)touchUpInsideResetButton:(id)sender
{
  [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" 
                                                      object:nil];
}

viewControllers reloadContents

+3

Source: https://habr.com/ru/post/1542631/


All Articles