开发者

Is it okay to call a web service in App Delegate?

I have a bit of a dilemma here. I have an app that has UINavigationViewController as the rootViewController. As far as I can tell, UINavigationViewController requires a rootViewController when instantiated. The problem is, I hav开发者_Go百科e several UIViewControllers to choose from and the choice depends on data returned from a web service.

Does this mean I should call the web service in AppDelegate and only instantiate the UINavigationViewController once I get the data?

I also thought about creating a wrapper UIViewController that contains the UINavigationViewController and its view as subview. But looks like nesting view controllers has some disadvantages, primarily on lifecycles and rotation.

Is there a better way to do this? Thanks!

UPDATE: My co-worker recommended another solution which could potentially be the cleanest. Here goes, AppDelegate loads a "bootstraper" ViewController which main purpose is to pull the data from webservice and maybe display a loading indicator. Once it gets the data, it will remove itself and add the Navigation controller with the appropriate root.


maybe push a viewcontroller with a "waiting for data..." view, and then when the web response arrives pop it and push the appropriate viewcontroller?


A Web service call might take a while. It's a very bad idea to freeze the UI while the Web takes its sweet time to respond. Load a view controller (and a view) with a spinning wheel and a "Please wait..." notice, initiate the service call, then once the service responds, replace it with the real thing.


The UINavigationController has a property called viewControllers that can be set. See http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/occ/instp/UINavigationController/viewControllers.

// after your web service returns
navigationController.viewControllers = [NSArray arrayWithObject:yourNewController];

If you are not going to show anything in the navigation controller until the web service returns then you could just wait until it returns before creating the navigation controller. If you have a different view controller that you want to show in the navigation controller

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜