- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 UIAlertView
,它会弹出一条消息和一个按钮“继续”。我想要的是摇动手势也会调用与点击继续按钮相同的代码。但是,我不确定这是否适用于 iOS7 中的 UIAlertView,或者我是否必须创建自己的自定义警报 View 。
我已经尝试实现:
(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { ... }
和
canBecomeFirstResponder { return YES; }
但是当它们在主视图设置为 becomeFirstResponder
时正常工作时,一旦 UIAlertView
弹出,它们就不会响应。那么,我尝试在 UIAlertView+MotionResponder.h
的类别中实现这些以尝试扩展 UIAlertView
,但是 motionEnded
没有被调用即使 canBecomeFirstResponder 正确返回 YES
。
那么这应该有效吗?还是我必须实现自己的警报 View ?
最佳答案
The
UIAlertView
class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
UIAlertView
的内部 View 层次结构比它看起来复杂得多,从 iOS 7 开始,它甚至没有添加到任何 UIWindow
中,因此不足为奇它没有像您预期的那样参与响应者链。
但是,您可能会考虑在 Controller 中实现 Action 识别逻辑,并让它触发 UIAlertView
关闭。
您只需要保持对 UIAlertView
实例的引用,检测 Controller 中的摇动手势并发送一个 –dismissWithClickedButtonIndex:animated:
消息到警报,在命令将其驳回。
当 UIAlertView
出现在当前 Controller 的顶部时,上述技术将不起作用,因为它会拦截摇动手势并且永远不会调用 Controller 方法。
要使其正常工作,您必须在窗口级别检测手势。这是您可以如何做的一个简短示例。
首先,我们在 UIWindow
上定义一个类别,它拦截摇动手势并广播通知(我将只发布 @implementation
,因为 @interface
为空)
@implementation UIWindow (Shake)
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"UIWindowDidShake" object:nil userInfo:nil];
}
}}
@end
然后在 Controller 中,我们在显示警报时注册到通知,并在通知到达时解除警报。最后,一旦警报被解除,我们就可以从观察者中移除 Controller 。
@property (nonatomic, strong) UIAlertView *alert;
...
- (IBAction)showAlert:(id)sender {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissAlert) name:@"UIWindowDidShake" object:nil];
self.alert = [[UIAlertView alloc] initWithTitle:nil message:@"An alert" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[self.alert show];
}
- (void)dismissAlert {
[self.alert dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIWindowDidShake" object:nil];
}
关于ios - 是否可以响应 UIAlertView 中的摇动手势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20341572/
这个问题已经有答案了: iOS How to dismiss UIAlertView with one tap anywhere? (2 个回答) 已关闭10 年前。 嗨,我是 Xcode 新手,ob
我试图在我的 swift 应用程序中显示一个 UIAlertView alert = UIAlertView(title: "", message: "bla",
问题是 iOS 键盘有时不再有反应。所以没有输入是可能的。如果我取消注释 clickedButtonAtIndex 中的第二个 UIAlertView,它工作正常。 原因可能在其他地方?我不知道...
我是 iPhone 开发人员的新人, 我想依次实现2个警报 View ,就像当用户按下删除按钮时,第一个警报 View 会询问你确定要删除吗?有两个按钮是 和否 现在,如果用户按 yes ,则第二个警
我想弄清楚为什么我的应用程序会崩溃。 它在带有 ios5.1 的模拟器中运行的 Xcode 4.4 中运行良好,但是当我切换到 xcode 4.5 和 ios6 时,我得到了 EXC_BAD_ACCE
我的 iOS 应用程序正在使用 Swift 2.0、RMUniversalAlert 版本 0.7 和 UIAlertView+Blocks 版本 0.9 当我在 Xcode 7 中构建它时,出现错误
我遇到了崩溃: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-
我在主线程上显示 UIAlertView 时遇到问题。我不知道为什么,但它不断崩溃,尽管我在主线程上运行。以下 block 位于后台线程上,但我在主线程上有警报,如下所示: void (^remove
我已将 iPhone 设置为横向模式,但当我尝试显示 UIAlertView 时,它会进入纵向模式。横向模式下如何显示? 最佳答案 在applicationDelegate.m文件的applicati
我使用 UIAlerView 子类创建自定义登录页面。现在,当我单击按钮时,它会打开 UIAlertView 我想根据按下的按钮更改主视图。 但是,由于 UIAlerView 的所有实现都在另一个类中
我弹出一个 UIAlertView 来询问用户是否确实要打开某个 URL。如果他们选择"is",我的应用程序会立即打开 URL 并发送到后台。当用户重新打开我的应用程序(使用多任务处理)时,警报 Vi
有没有办法将 .tag 添加到 UIAlertView 按钮?原因是,我向警报添加了一些动态按钮,这些按钮有时会出现在警报中,有时不会。我认为最好的方法是添加标签。有更好的方法吗? 警报中始终出现的选
我在 ib 中创建了一个自定义 uialertview,带有两个按钮和一个文本字段。然后我以这种方式创建一个 .h 和一个 .m: #import @interface DialogWelcome
看一下下面的代码,问题是AlertView没有显示出来,即使我在调试中看到代码已被执行。 请指教。 非常感谢 埃兰 -(void)displayABC:(id)sender { sta
我正在尝试更改 uialertview 的背景。一切工作正常,除了我添加到 uialert View 的背景图像不会在警报 View 内缩放。所以基本上你只能看到基于当前大小的背景图像的一部分。有什么
在我的应用程序中,我使用验证 key 通过 Wi-Fi 从服务器下载内容。如果许可证 key 错误或 wi-fi 不可用,我需要显示 UIAlert。我已经编写了用于显示警报 View 的男女混合代码
我创建了一个带有两个按钮和一个文本字段的 uialertview,如果文本字段为空,我希望禁用“确定”按钮。我如何获取 uibutton 对象来更改启用状态。 最佳答案 您可以使用从 UIContro
iPhone 应用程序开发人员您好, 我正在开发一个 iPhone 应用程序。该应用程序允许用户将图像上传到我的服务器。我想在alertView中显示上传进度。我需要一些示例代码来说明如何实现带有进度
有没有办法以编程方式隐藏 UIAlertView。我正在我的进度 View 中显示进度 View ,当进度 View = 1.0 时,我想隐藏 UIAlertView。 最佳答案 您可以做的是观察(使
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Is it possible to lay out 2 buttons vertically on UIAlertV
我是一名优秀的程序员,十分优秀!