1

I have a view that uses a modal view with a page curl to allow for a username to be entered. This username is then verified with a web-based service to see if it is valid.

Everything works great until you enter an invalid username and click outside the modal view. This still checks the username, which is reported invalid and a UIAlertView opens. However, it goes back to the parent view.

Is there any way to get the modal to not dismiss in this case?

I have tried to reload the view but either it isn't working or the UIAlertView is blocking it. The last idea I have is to couple displaying the modal view with the "OK" on the alert for an invalid username. Anyone have any ideas?

jscs
  • 63,694
  • 13
  • 151
  • 195
Mike
  • 13
  • 3
  • There should be no problem in showing alert above the modal view. Why your modal view dismissed on tap outside of the view? – Ariel Nov 29 '11 at 21:40
  • The click is outside the modal on the parent view which dismisses the modal. The dismissal triggers the username check which displays the UIAlert. Which all works great unless they put in an invalid username, then the modal closes, the alert displays but I would like it to redirect back to the modal view. This seems like it should be simple, but I have spent a lot of time and it just isn't working for me. – Mike Nov 29 '11 at 21:54

2 Answers2

1

If you were not using a UINavigationController You could put something like this in the view controller that calls the modal view:

-(void)dismissModalViewControllerAnimated:(BOOL)animated{
    if (_someFlagForBeingProperlyLoggedIn) [super dismissModalViewControllerAnimated:animated];
}

When you tap on the page curl the presenting/parent view controller is sent dismissModalViewControllerAnimated:.

Since you are using a navigation controller your options are limited. This is because UINavigationController is a subclass of UIViewController, and a self centered one at that. When you click the page curl it's dismissModalViewControllerAnimated: is being called.

You still have the option of subclassing UINavigationController and implementing the above method, but that will get messy in a hurry.

Having the UIAlertView "direct back" to the modal login view IS very easy. Have that main view conform to the UIAlertViewDelegate protocol. When you display the alert set that instance as the delegate, and in that class implement the method:

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    // Enclose in if (buttonIndex == #) for selective calling
    UINavigationController* nav = (UINavigationController*)[[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"Preferences"];
    [nav setModalTransitionStyle:UIModalTransitionStylePartialCurl];
    [self.navigationController presentModalViewController:nav animated:YES]; 
}

Then when the alert view is dismissed it will show the 'login' view.

NJones
  • 27,139
  • 8
  • 70
  • 88
  • I tried calling that function with a simple NSLog but it didn't even hit when the modal view closed. I do like your idea of just setting a flag. That way once the modal view closes I could check that flag in the viewDidAppear of the parent. Still not sure why that function isn't being called though. Thank you for the idea. – Mike Nov 29 '11 at 21:44
  • When you click on the page curl of the, let's call it login view controller, it calls `dismissModalViewControllerAnimated:` on the presenting/parent view controller not on the login view controller. – NJones Nov 29 '11 at 22:30
  • Correct. I added that to the presenting view controller and then put in there a NSLog(@"Dismissing Modal"); and it never shows up in the debugger. Here is the code that calls the modal. UINavigationController* nav = (UINavigationController*)[[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"Preferences"]; [nav setModalTransitionStyle:UIModalTransitionStylePartialCurl]; [self.navigationController presentModalViewController:nav animated:YES]; – Mike Nov 29 '11 at 22:51
0

You should redisplay your modal view with a little delay, something about 0.3-0.5. that's the amount of time needed to alert to be dismissed and that is exactly animation(the dismissing of the alert view) that prevent the modal view from showing up.

-(void)showModal{
    SomeModalViewClass* modalView = [[SomaModalViewClass alloc]init];
    [self setModalTransitionStyle:UIModalTransitionStylePartialCurl];
    [self presentModalViewController:modalView animated:YES];
    [modalView release];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    //check the button index if needed and then
    [self performSelector:@selector(showModal) withObject:nil afterDelay:0.3]; 
}
Ariel
  • 2,430
  • 1
  • 17
  • 20