use*_*064 -1 iphone objective-c ios
我想显示一个约2秒的临时警报,让用户有机会取消操作.如果用户单击取消按钮,则不会调用后续方法.但是,如果用户没有按取消,则会调用后续方法.任何人对如何使这项工作有任何想法?
我有一个看起来像这样的设置:
-(void) buttonPress {
//alert with message and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"2 seconds to cancel"
message:@"Push Cancel to stop" delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:nil, nil];
alert.tag = 1;
[alert show];
return;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0 && alertView.tag == 1) {
//action cancelled
}
else {
//action not cancelled
}
}
Run Code Online (Sandbox Code Playgroud)
尝试此操作以在2秒后解除警报:
-(void) buttonPress {
//alert with message and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"2 seconds to cancel"
message:@"Push Cancel to stop" delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:nil, nil];
alert.tag = 1;
[alert show];
// Create the perform selector method with alert view object and time-period
[self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:2];
return;
}
// Dismiss the alert view after time-Period
-(void)dismissAlertView:(UIAlertView *)alert
{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0 && alertView.tag == 1) {
//action cancelled
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(dismissAlertView:) object:alertView];
}
else {
//action not cancelled
}
}
Run Code Online (Sandbox Code Playgroud)