Berkan Ercan
Berkan Ercan

Reputation: 1207

Loading screen for iOS app

I need a loading screen to show while some data is being downloaded from the server. I present a view controller with modal segue without animation. But I don't know how to dismiss the loading screen view controller since dismissViewController function can only be called from inside.

I should be able to dismiss the loading screen view controller from another view controller. Any suggestions?

Upvotes: 0

Views: 1336

Answers (2)

Midhun MP
Midhun MP

Reputation: 107121

One option is using NSNotificationCenter. You can post custom notification and listen it in loading view for closing it.

If you are getting the data in parent view then you can close the loading view from parent view also. You can call dismissViewController from parent view using presentingViewController property of UIViewController class.

In your case from parentView you can dismiss the child view using:

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

presentingViewController

The view controller that presented this view controller. (read-only) Declaration

Swift

var presentingViewController: UIViewController? { get }

Objective-C

@property(nonatomic, readonly) UIViewController *presentingViewController

Discussion

When you present a view controller modally (either explicitly or implicitly) using the presentViewController:animated:completion: method, the view controller that was presented has this property set to the view controller that presented it. If the view controller was not presented modally, but one of its ancestors was, this property contains the view controller that presented the ancestor. If neither the current view controller or any of its ancestors were presented modally, the value in this property is nil. Import Statement

import UIKit Availability

Available in iOS 5.0 and later.

Upvotes: 2

RuiMochila
RuiMochila

Reputation: 185

Do you need to show another VC? You could just show a subview, making it visible=true when you start the app, and on the request success or failure callbacks hide it again (depending on the networking framework you are using)

I don't know were you start the request and were you know when it is done, if the loading screen knows when the download is finished you can do one of two things, or you pass the first VC as a delegate (defining a protocol) to the second VC (loading screen), and when you know on the loading screen that the download is finished you call a method on the delegate that will dismiss the loading screen,

Or you can use NSNotificationCenter, register for some kind of events on the first VC and when the process finishes on the loading screen you notify the first VC with this method, to dismiss the loading screen.

If you start the process on the first VC, and you know when it ends also on the first VC I would not understand your question, as you would just dismiss the loading screen and it would work. Could you give more info? If you'd like I can post some code in order to help you in one of this approaches.

Upvotes: 1

Related Questions