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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With