I'm trying to develop an application where it allows user logins with AFNetworking. I have the database set up correctly and everything seems to be working fine except when the user first logs on.
What I have is very simplistic:
[[API sharedInstance] commandWithParams:params
onCompletion:^(NSDictionary *json) {
//result returned
NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0];
if ([json objectForKey:@"error"]==nil && [[res objectForKey:@"UserID"] intValue]>0) {
[[API sharedInstance] setUser: res];
[self performSegueWithIdentifier:@"Login" sender:self];
} else {
//error
[UIAlertView title:@"Error" withMessage:[json objectForKey:@"error"]];
}
}];
Basically, the above code returns the result for a user login details, successful only if login details match. As you can see above, I clearly set the user in the sharedInstance, used throughout the application. After the user is set I attempt to perform the segue, as that's what a login button should do.
The segue does occur and the program is operational, but there's 2 issues I can't work out and I've been spending hours trying to fix it. First I get the message in Xcode's output window:
Warning: Attempt to present UITabBarController on LoginVC while a presentation is in progress!
And secondly, to test that the user was set correct, on the Profile screen (the first screen segued to) have a label that has it's text set to:
NSString stringWithFormat:@"Welcome %@",[[[API sharedInstance] user] objectForKey:@"username"]];
And it's a hit or miss whether the username actually appears or says (null) as shown:
You can also see a button I made there, to print to the output window the values of user. Even when the message is "Welcome (null)" and I press the button, all the values are correctly there so I'm unsure why the string sometimes states null.
Here's the storyboard setup regarding the issue I'm having:
If anyone could help me on this one I would truly appreciate it.
As you correctly diagnosed, the problem is definitely some strange and/or duplicative invocation of the segues. Looking at your project, the problem is that your "Login" button has both an IBAction
method as well as a segue from that login screen to the next controller (the tab bar controller):
That's a problem, because the segue will be triggered when you hit the button, but will be triggered again by your - (IBAction)login:(id)sender
code.
Because you have an IBAction
, the segue should not be from the button to the next controller, but rather from the controller itself. Thus, delete your existing Login
segue from the button to the next screen and recreate it from the controller itself:
Give that new segue the "Login" identifier, and now your "Login" button will not automatically perform the segue itself, but will let the IBAction
do its work and manually performSegueWithIdentifier
as appropriate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With