Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling method in current view controller from App Delegate in iOS

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.

like image 492
yupyupyup Avatar asked May 03 '11 17:05

yupyupyup


People also ask

What is app delegate in iOS?

So an app delegate is an object that the application object can use to do certain things like display the first window or view when the app starts up, handle outside notifications or save data when the app goes into the background.

What is app delegate and methods in Swift?

The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.

Does SwiftUI have AppDelegate?

SwiftUI's App protocolStarting with iOS 14, Apple introduced the App protocol for pure SwiftUI applications which mostly replaced the AppDelegate and SceneDelegate. A most basic SwiftUI App implements the body property as the entry point of a SwiftUI application.


2 Answers

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.

like image 93
Cyprian Avatar answered Sep 28 '22 04:09

Cyprian


If you are targeting iOS 4.0 and later, you can use the window's rootViewController property to get the current view controller.

[window.rootViewController viewWillAppear]; 

If you want your application to run on versions prior to iOS 4.0, then you could add an instance variable to the application delegate to remember which view controller called the upload method, having the controller send itself as a parameter.

- (void)upload:(UIViewController *)viewController {     self.uploadingViewController = viewController; // This is the property you add     ... } - (void)connectionDidFinishLoading:(NSURLConnection *)connection {     [self.uploadingViewController viewWillAppear];     self.uploadingViewController = nil; } 

You should also consider using a different method to reload the buttons, something like reloadButtons, since it is not related to the view appearing in this case. You would then call that method from within viewWillAppear.

like image 25
ughoavgfhw Avatar answered Sep 28 '22 05:09

ughoavgfhw