How to reload UIViewController

2020-07-10 19:49发布

问题:

I want to reload all the views contained in tabbar controller(UIViewController).After searching I found that I've to apply setNeedsDisplay Method but I am not able to get where should I apply it.Any other alternatives are also welcomed

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    .....
    .....

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    [self customToolbar];
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];    
    return YES;
}
-(void)customToolbar
{
    //Declared view controllers and their Navigation Controller
    .....

    //Declared tab bar items
    .....    

    tabBarController = [[GTabBar alloc] initWithTabViewControllers:viewControllersArray tabItems:tabItemsArray initialTab:1];
}

回答1:

The correct way to do this would be to add any VC that needs to be refreshed as an observer to a certain NSNotificationCenter notification name. Once the VC gets this message, just call a selector that calls [self setNeedsDisplay].

To add a VC to NSNotificationCenter:

[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(setNeedsDisplay) name:@"ViewControllerShouldReloadNotification" object:nil];

Don't forget to call removeObserver:self when the view controller is deallocated.