iPhone Popping View Controller before ViewDidAppear
In my app I have a login and if the login is successful they user gets a session id. When the user's session expires and they r开发者_运维技巧equest a page with the expired session the backend reports that the session is expired and the user needs to log back in and my app calls the popToRootViewControllerAnimated
(login page). I am using the NSURLConnection
class to schedule callbacks to my UIViewController
for when the downloading is complete. The problem that I am running into is that I schedule the NSURLConnection
during my viewDidLoad
and the connection reports back before the view controller appears. I would put the NSURLConnection
in the viewDidAppear
but I don't want to contact the server every time the view appears. Does anyone know what the best way to fix this problem?
First, declare an enum variable:
typedef enum {
StatusLoggedOut,
StatusAttemptingLogin,
StatusLoggedIn
} LoginStatus;
Then add a member variable to your view controller to store the login status. Then, inside viewDidAppear: you can do this:
- (void)viewDidAppear:(BOOL)animated
{
if (loginStatus == StatusLoggedOut) {
loginStatus = StatusAttemptingLogin;
// start the NSURLConnection
}
}
When the login completes, you can switch the status to StatusLoggedIn or StatusLoggedOut (depending on how it turned out).
You could get away with a simple BOOL but experience has taught me that it's better to represent all the in-between states.
Also, you didn't ask about this, but I think it would be much better user experience if you presented the login screen as a modal view; using popToRootViewController
will cause the user to lose his place in your app, which is annoying. (Unless you are saving and restoring it, in which case you are making a lot of work for yourself.)
精彩评论