Search code examples
iphoneobjective-cxcodeuiimageviewtouchesbegan

touchesBegan to dismiss a view


I have a ViewController with a full screen UIImageView. I've hidden statusBar and NavigationBar, so there is no way to go back but tapping somewhere.

So i was thinking to go back just using this touchesBegan

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    [self dismissModalViewControllerAnimated:YES];
}

I also tried to use this way

-(void)viewDidLoad 
{
    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self.navigationController setNavigationBarHidden:YES];

    UITapGestureRecognizer *tapRecognizer;
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissModalViewControllerAnimated:)];
    [imageFrame addGestureRecognizer:tapRecognizer];

    imageFrame.image = [UIImage imageWithContentsOfFile:image];

    [tapRecognizer release];
}

I got stuck there, with this full screen image and i can't go back. How can I dismiss the viewcontroller?


Solution

  • If you are using a navigation bar I'm assuming that you did a pushViewController to start this view controller? In which case you would want

    [self.navigationController popViewControllerAnimated:YES];
    

    to dismiss the controller.