简体   繁体   中英

How can I elegantly dismiss view if at the moment of dismissing I don't know that view is fully created?

How can I elegantly dismiss view if at the moment of dismissing I don't know if that view is fully created ?

Here is my dilemma. The view is showing some data that is requested from the server. Therefore I asynchronously send server request from viewDidLoad and wait in two observers if request failed or not.

For "dismissing" view I use exit segue (storyboard).

So there are two scenarios:

  • Server-side error occurred after viewDidAppear was called. Then I can call performSegue or self.navigationController.pop... and everything is nice.
  • Server-side error occurred before viewDidAppear was called. How can I navigate user to previous view in this case ?

For now I just put server request to viewDidAppear but it's very uncool because user looks at empty view for significant amount of time :(

How would you deal with this ?

UPDATED Make a flag, ie

@property (nonatomic, assign) BOOL requestFailedOrViewDidAppear;

set it to NO at start of viewDidLoad or in init method. Then in your server-side error handler do something like this:

if (requestFailedOrViewDidAppear) {
    //return to previous view
}
else {
    requestFailedOrViewDidAppear = YES;
}

Put the same code into viewDidAppear, too.

That is more elegant in terms of implementation. However if you want it to be elegant in terms of UI/UX, replace "//return to previous view" with code that creates and shows a UIAlertView explaining the error. Then, when user dismisses that alert view, you can return to the previous view.

Assuming that your user will expect some info to be shown on the view that's being presented, If the request fails before viewDidAppear , I would wait until view appears, show the error, and dismiss the view. Else if request fails after viewDidAppear , I would just show the error and dismiss the view on the request observing method. That would be my way of dealing with this problem.

Turned out there is int flag in UIViewController that is called _appearState . When it's 2 that means view has appeared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM