gpt4 book ai didi

iOS:添加到 UIAlertView 的 GestureRecognizer 不调用其操作

转载 作者:行者123 更新时间:2023-11-29 03:04:40 25 4
gpt4 key购买 nike

我有一个 UIAlertView ,它会在几秒钟后被计时器关闭。有些用户希望通过点击警报 View 本身来提前关闭警报 View 。
因此,我尝试向alertView添加单击手势识别器,但未调用其操作。然后我继续读到,在添加手势识别器之前,警报 View 必须位于 View 层次结构中。我对此不确定,但无论如何,我因此将手势识别器添加到了警报 View 委托(delegate)方法 didPresentAlertView: 中,但该操作仍然没有被调用。
这是我的代码。任何帮助表示赞赏。

告警 View 的初始化:

alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
}
[alertView performSelectorOnMainThread:@selector(show) withObject:self waitUntilDone:NO];
[self performSelector:@selector(dismissAlertAfterDelay:) withObject:alertView afterDelay:4.0];

添加手势识别器的委托(delegate)方法:

- (void)didPresentAlertView:(UIAlertView *)alertView{
alertView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(dismissAlert:)];
singleTap.numberOfTapsRequired = 1;
[alertView addGestureRecognizer:singleTap];
}

以及未调用的操作方法:

-(void)dismissAlert:(UITapGestureRecognizer *)sender{
UIAlertView *alertView = (UIAlertView *)sender.view;
[alertView removeFromSuperview];
}

我知道警报 View 显示在单独的窗口中,但我相信无论如何,当为警报 View 启用用户交互时,添加到其中的手势识别器应该调用操作方法。
有什么建议吗?

最佳答案

UIAlertView 是 iOS 7 中的模型类,尽管它的名称以 View 结尾。它永远不会添加到 View 层次结构中。这就是为什么向它添加 subview 也不起作用的原因:它们被添加了,但它们的父 View 不在 View 层次结构中。显示警报时显示的 View 层次结构是私有(private)的,不能乱用。

我建议仅使用关闭按钮来关注 HIG。如果您真的非常希望能够窃听整个警报,请查看 SDCAlertView .这是我编写的一个 UIAlertView 克隆,它确实像一个实际 View 。

关于iOS:添加到 UIAlertView 的 GestureRecognizer 不调用其操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22919898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com