2

嗨,我是 Xcode n Objective-C 的新手,在这方面需要帮助。我有一个无按钮 UIAlertView,它会在点击自身时被关闭。但我只想在点击警报框外部而不是里面时关闭它。谢谢

 //generating the alertview with no button

 +(void) showAlertNoButtons:(NSString*)title text:(NSString*)text{

    UIAlertView* alertView = [[UIAlertView alloc]
                          initWithTitle:title message:text
                          delegate:self cancelButtonTitle:nil
                          otherButtonTitles:nil];

   UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                    initWithTarget:self
                                    action:@selector(dismissAlert:)];

   alertView.delegate=self;
   [alertView addGestureRecognizer:singleTap];
   [alertView setMultipleTouchEnabled:YES];
   [alertView setUserInteractionEnabled:YES];

  [alertView show];
 }
   //
  +(void)dismissAlert:(UITapGestureRecognizer *)gesture
    {
      UIAlertView* alertView =(UIAlertView *)gesture.view;
      [alertView dismissWithClickedButtonIndex:0 animated:YES];

    }
4

1 回答 1

2

你需要打电话- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated

当您点击其他任何地方时

这段代码将处理你的水龙头

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [self dismissWithClickedButtonIndex:index animated:YES];
}
于 2013-03-21T06:00:23.193 回答