Can you stop a modal view from dismissing?

1.7k Views Asked by At

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?

2

There are 2 best solutions below

3
On

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.

0
On

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]; 
}