Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>According to the <a href="https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html" rel="nofollow">documentation</a></p> <blockquote> <p>The <code>UIAlertView</code> 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.</p> </blockquote> <p>The internal view hierarchy of <code>UIAlertView</code> is much more complicated than it appears and starting from iOS 7 it is not even added to any <code>UIWindow</code>, so it's no big surprise that it doesn't participate to the responder chain as you expect.</p> <p>However, you might consider of implementing the motion recognition logic within your controller instead and to have it triggering the <code>UIAlertView</code> dismissal.</p> <p>You just need to keep a reference to the <code>UIAlertView</code> instance, detect the shake gesture in the controller and send a <code>–dismissWithClickedButtonIndex:animated:</code> message to the alert, in order to dismiss it.</p> <h2>EDIT</h2> <p>The above technique won't work when the <code>UIAlertView</code> is presented on top of the current controller since it will intercept the shake gesture and the controller method will never be called.</p> <p>To make it work, you have to detect the gesture at a window level. Here's a brief example of how you can do.</p> <p>First we define a category over <code>UIWindow</code> that intercepts the shake gesture and broadcasts a notification (I'll just post the <code>@implementation</code>, since the <code>@interface</code> is empty)</p> <pre><code>@implementation UIWindow (Shake) - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (event.type == UIEventTypeMotion &amp;&amp; event.subtype == UIEventSubtypeMotionShake) { [[NSNotificationCenter defaultCenter] postNotificationName:@"UIWindowDidShake" object:nil userInfo:nil]; } }} @end </code></pre> <p>then in the controller we register to the notification when showing the alert and we dismiss the alert when the notification arrives. Finally we can remove the controller from the observers as soon as the alert has been dismissed.</p> <pre><code>@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]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload