Ios – Calling method in current view controller from App Delegate in iOS

iosiphoneobjective c

I have two view controllers (BuildingsViewController and RoomsViewController) that both use a function within the App Delegate called upload. The upload function basically does an HTTP request, and if its successful or unsuccessful, triggers a uialertview. This is working fine.

The part I'm struggling with is from within the app delegate's connectionDidFinishLoading method. I need to be able to basically refresh the current view controller via perhaps viewWillAppear method of that view controller. Inside the viewWillAppear function of each view controller I have code which determines the buttons on the bottom toolbar.

I want the "upload" button in the toolbar of each view controller to automatically be removed when the uploading is done via the app delegate.

I've tried doing [viewController viewWillAppear:YES] from within the connectionDidFinishLoading method of the app delegate, but it never gets called.

I hope I'm clear enough. Any help is greatly appreciated.

Thanks.

Best Answer

To do the refresh of the view do not call viewWillAppear if the view is already displayed. What you want to do is the following:

When ConnectionDidFinishLoading method is triggered post a notification

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

In your viewController observe for this notification. You do it by adding this code to your init or viewDidLoad method

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

Now implement -(void)refreshView:(NSNotification *) notification method in your viewController to manage your view to your liking.