0

I am trying to instruct my app to open a certain view, depending on whether the user has already created a user profile on my database.

so basically -

- (void) viewWillAppear:(BOOL)animated {

//all my ASIHTTPRequest code here
//blah blah blah

NSString *responseString = [request responseString];

if ([responseString isEqualToString:@"noexistingdata"])
{

FriendsViewController *friendsview = [[FriendsViewController alloc] initWithNibName:nil bundle:nil];

//SOMETHING NEEDS TO GO HERE TO MAKE THIS WORK!

} else if ([responseString isEqualToString:@"success"])
{

//do whatever

}

}

I just want the most basic code for changing a view... I would try using IBAction, but obviously that won't work as this is in a void for the app's launch (rather a response to a button the user presses), also thought about void in a void, which also did not work.

So basically what I need is:

Launch App > App receives response from my server > (IF RESPONSE = "THIS", LOAD VIEW "X") (IF RESPONSE = "THAT", LOAD VIEW "Y")

Anyone have a clue?

PS: would it be better to list this is applicationDidFinishLaunching?

Chris G
  • 11
  • 1
  • 1
  • 2

1 Answers1

0

-viewWillAppear: is called just before a view is displayed. That's probably not a great time to a) run some synchronous network request and b) load another view controller.

I'd suggest having the application delegate create a view controller who's view just displays a message, like "Connecting to server" or "Retrieving user credentials" or whatever. You might add an animated spinner to let the user know that they're supposed to wait, if that's all they're allowed to do at this point. Then, after the view is displayed, such as in that controller's -viewDidLoad method, start an asynchronous network connection to talk to the server. When you get back a response, decide which view controller to instantiate next, and do that.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Awesome, ok that makes sense, but still, Let's just assume the above code is now "- (void) viewDidLoad {", what would the code be to change views in a viewDidLoad void? – Chris G Mar 21 '11 at 01:25
  • Start the network request in -viewDidLoad. When the request completes, switch to the next view. How you switch to the view depends on your app. If you're using a nav controller, just push the new view controller. If not, make it's view a subview of the window as is done in your -application:didFinishLaunchingWithOptions:. – Caleb Mar 21 '11 at 02:06