title: Close UIView when UIButton is tapped
body: <p>In my app I have a Horizontal Image Gallery wherein when I tap the image, <code>UIView</code> is called displaying enlarged images. The <code>UIView</code> contains, a <code>scrollView</code>, <code>imageviews</code> (dynamically created) and a <code>UIButton</code> (close button) programmatically created. However, when I tap the close button <code>UIView</code> is not dismissed.
  In my <strong>viewDidLoad</strong> I set up the <code>gestureRecognizer</code>.</p>
  
  <pre><code>[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)]];
  </code></pre>
  
  <p>Here's my <strong>singleTapAction</strong> method:</p>
  
  <pre><code>- (void)singleTapAction:(UIGestureRecognizer *)singleTap_
  {
      MTGalleryPopUp* galleryImage = [[[NSBundle mainBundle] loadNibNamed:@"MTGalleryPopUp" owner:self options:nil] lastObject];
      if(galleryImage.hidden)
      {
          NSLog(@"HIDDEN");
          [galleryImage dismissWithAnimation:YES];
      }
      else
      {
          NSLog(@"NOT HIDDEN");
          [self showGalleryDialog];
      }
  }
  </code></pre>
  
  <p>Here's my <strong>showGalleryDialog</strong> method:</p>
  
  <pre><code>- (void)showGalleryDialog
  {
      MTGalleryPopUp * galleryView = [[[NSBundle mainBundle] loadNibNamed:@"MTGalleryPopUp" owner:self options:nil] lastObject];
  
      [galleryView setReferenceVC:self];
      [galleryView initWithTitle:@"Image Gallery"];
      [galleryView setAlpha:0.0f];
      [self.view addSubview:galleryView];
      [UIView beginAnimations:nil context:nil];
      [galleryView setAlpha:1.0];
      [UIView commitAnimations];
  }
  </code></pre>
  
  <p>Here's how i am creating the <code>UIView</code>:</p>
  
  <pre><code>- (id)initWithTitle:(NSString *)aTitle
  {
      CGRect rect = [[UIScreen mainScreen] bounds]; // portrait bounds
      if (self = [super initWithFrame:rect])
      {
          CGRect screenRect = [[UIScreen mainScreen] bounds];
          [self setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8f]];
  
          self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, TITLE_HEIGHT, screenRect.size.width, 350)];
          [self.scrollView setUserInteractionEnabled:YES];
  
         CloseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
         [CloseButton addTarget:self
                      action:@selector(dismissWithAnimation:)
            forControlEvents:UIControlEventTouchUpInside];
  
          [CloseButton setTitle:@"X" forState:UIControlStateNormal];
          [CloseButton.layer setBorderWidth:2];
          [CloseButton.layer setBorderColor:[UIColor whiteColor].CGColor];
  
          CloseButton.layer.cornerRadius=10;
          CloseButton.layer.masksToBounds = YES;
          [CloseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
          CloseButton.titleLabel.font=[UIFont systemFontOfSize:16];
          CloseButton.frame=CGRectMake(self.frame.size.width-80, 8, 20, 20);
          [self addSubview:CloseButton];
  
          strURL = [NSString stringWithFormat:@"%@%@?temp_url_sig=%@&amp;temp_url_expires=%lld", URLstorage, CONTAINER_PATH, HMACStr, [@(floor([oneMinLater timeIntervalSince1970])) longLongValue]];
  
          [mutableURLstorage addObject:strURL];
          [imagesArray addObject:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]]];
      }
  
      CGSize pageSize = CGSizeMake(ITEM_WIDTH, self.scrollView.frame.size.height);
      _pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, screenRect.size.height-30, 320, 36)];
      _pgCtr.backgroundColor=[UIColor grayColor];
      int numberofPage=ceil((float)[imagesArray count]/2.5);
      _pgCtr.numberOfPages= numberofPage;
      self.scrollView.contentSize = CGSizeMake(450*numberofPage, pageSize.height);
  
      int imgCurrentX = 10;
      for (UIImageView* image in imagesArray)
      {
          @autoreleasepool
          {
              imageview = [[UIImageView alloc] initWithFrame:CGRectMake(imgCurrentX, 45, 200, self.scrollView.frame.size.height - 10)];
                  imageview.image = (UIImage*)image;
                  [imageview setUserInteractionEnabled:YES];
                  [self.scrollView addSubview:imageview];
                  imgCurrentX = imgCurrentX + 220;
              }
          }
  
          [self addSubview:self.scrollView];
      }
  
      return self;
  }
  </code></pre>
  
  <p>When I'm on debug, what happens is it goes back to singleTap_ method and recreates again the <code>UIView</code> (now two <code>UIViews</code> are overlapping). What am I doing wrong here?</p>
  
  <p>Any help will be very much appreciated. Thanks.</p>