Pregunta

I want to make it so that when you press a button, an alert pops up for a second or two and then disappears on its own. Is there any way to do this?

¿Fue útil?

Solución

The accepted answer seems a little dangerous. From the docs (emphasis mine)

A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.

This selector takes two arguments and the argument is a primitive not an id. So this appears to be working by coincidence...

A safer way that I think reads better as you call the method in the normal way but just use gcd for scheduling:

__weak __typeof(alertView) weakAlertView = alertView;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  [weakAlertView dismissWithClickedButtonIndex:0 animated:YES];
});

Otros consejos

Try using this after the line which shows your alert view:

[alertView performSelector:@selector(dismissWithClickedButtonIndex:animated:) withObject:[NSNumber numberWithInt:0] afterDelay:2];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top