Когда пользователь нажимает кнопку, на делегате UIAlertView
вызывается метод alertView:didDismissWithButtonIndex:
, поэтому вы должны выполнять свою работу
Сначала вам может понадобиться сохранить ссылку на таймер, чтобысоздайте новый ivar
// .h
@property (nonatomic, retain) NSTimer *alertViewTimer;
//.m
@synthesize alertViewTimer = _alertViewTimer;
- (void)dealloc;
{
[_alertViewTimer release];
//.. Release other ivars
[super dealloc];
}
- (void)playSound:(NSString *)filename
volume:(float)volume
ofType:(NSString *)type
subtitle:(NSString *)text
speed:(float)speed
loops:(NSInteger)loops
{
// .. the rest of your method
self.alertViewTimer = [NSTimer scheduledTimerWithTimeInterval:duration
target:self
selector:@selector(dismissAlert)
userInfo:nil
repeats:NO];
// .. the rest of your method
}
Затем в методе делегата реализуйте закрытие и аннулирование таймера:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[alertViewTimer invalidate];
self.alertViewTimer = nil;
// .. Do whatever else you want to do.
}
, и вы можете оставить свой dismissAlert
метод немного проще
- (void)dismissAlert
{
[self.currentAlert dismissWithClickedButtonIndex:0 animated:YES];
}
Решение 2
Другой потенциальный способ сделать это - заменить это:
[NSTimer scheduledTimerWithTimeInterval:duration
target:self
selector:@selector(dismissAlert)
userInfo:nil
repeats:NO];
на:
[self performSelector:@selector(dismissAlert) withObject:nil afterDelay:duration];
и затем реализовать
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(dismissAlert)
object:nil];
// .. Do whatever else you want to do.
}