Open modal view and dismiss on iPhone
How do I display 开发者_StackOverflow社区a full screen modal view and then if the user touches any where on the view, the view removes itself.
you can present a modal view that has a custom button as the background, and then when you press the button, or "background", you can call [self dismissModalViewControllerAnimated:YES];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
if ([touch tapCount] == 2) {
/* 2 touches here, you can dismiss your view */
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
if ([touch tapCount] == 1) {
/* 1 touch, dismiss your view */
}
}
If you were doing this inside of a subclass of UIViewController you could do something like this. This would trigger the modal to appear when the view is initially loaded, and trigger the modal to disappear when the screen is tapped.
-(void) viewDidLoad{
UIViewController *modalViewController = [[UIViewController alloc] init];
[modalViewController addTarget:self action:@selector(_dismissModal) forControlEvents:UIControlEventTouchUpInside];
[self presentModalViewController:modalViewController animated:YES];
}
-(void)_dismissModal{
[self dismissModalViewControllerAnimated:YES];
}
- Put a custom UIButton on top of everything on the view you want to present modally. (A custom UIButton is transparent)
- Hook up the UIButton's Touch Up Inside with one of the view's method (so that it can inform its delegate view controller about the event).
- Implement a corresponding method in the delegate view controller so the modal view can be dismissed when the delegate hears from it.
精彩评论