title: UIWindow addSubview handle events
body: <p>I want to build a <strong>custom alert</strong> by <code>UIViewController</code> and add its view as a subview to the current window. The view displays very well, but I can't handle any touch events.</p>
  
  <p>I have tried many times to add a button to this viewcontroller and add a target. I have also tried adding a <code>UITapGestureRecognizer</code> and set view's <code>userInteractionEnabled</code> to <code>true</code>, but this also failed even when I cleared all subviews just left the button.</p>
  
  <p>Have I missed something?</p>
  
  <p>Here is the code:</p>
  
  <h1>CustomAlert Viewcontroller:</h1>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view.
  
      self.view.backgroundColor = [UIColor clearColor];
  
      backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
      backgroundView.backgroundColor = [UIColor blackColor];
      backgroundView.alpha = 0.5;
      [self.view addSubview:backgroundView];
  
  
      backImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 300) / 2, (self.view.frame.size.height - 250) / 2, 300, 250)];
  
      backImage.image = [UIImage imageNamed:@"AlertBackground"];
      [self.view addSubview:backImage];
  
      confirmButton = [[UIButton alloc]initWithFrame:CGRectMake( backImage.frame.origin.x + 100 , backImage.frame.origin.y + 250 - 40, 100, 26)];
      [self.view addSubview:confirmButton];
      [confirmButton addTarget:self action:@selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
      confirmButton.backgroundColor = [UIColor redColor];
      [confirmButton setTitle:@"click me" forState:UIControlStateNormal];
  }
  
  -(void)confirmButtonClick:(UIButton*)sender{
  
      [self.view removeFromSuperview];
  }
  
  -(void)show{
      UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
      [window addSubview:self.view];
  }
  </code></pre>
  
  <h1>Method call custom alert:</h1>
  
  <pre><code>CustomAlert * alert = [[CustomAlert alloc]init];
  [alert show];
  </code></pre>