0

I have the code to make it so a notification pops up when I press a button. I want it so it appears when 10 seconds is up.

I know i need to do this:

(NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:
(NSInvocation *)invocation repeats:(BOOL)repeats

My action is:

{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                                message:[NSString stringWithFormat:@"%d", hi]
                                                   delegate:hi
                                          cancelButtonTitle:@"Ok"
                                          otherButtonTitles: nil];
    [alert show];


}
4

3 回答 3

1

You can just do:

[self performSelector:@selector(actionMethodName) withObject:nil afterDelay:10];

If you replace actionMethodName with the true method name.

If you want to use the timer route (which is perfectly valid too) it's easier to use scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: than to create an invocation. If you do this, keep a reference to the timer and invalidate it when you're done.


Based on the updated question details, using a timer and invalidating it is becoming a more preferable method, cancelling perform selector is doable but it isn't as clear as the timer route. I'll show the code for the perform selector route anyway:

- (void)startEverything
{
    [self performSelector:@selector(showAlert) withObject:nil afterDelay:10];
}

- (void)handleButtonTap:(id)sender
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showAlert) object:nil];
}

- (void)showAlert
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                                message:[NSString stringWithFormat:@"%d", hi]
                                                   delegate:nil
                                          cancelButtonTitle:@"Ok"
                                          otherButtonTitles:nil];
    [alert show];
}

Using timers would have the same method structure, but would require a @property to store the timer and instead of cancelPreviousPerformRequestsWithTarget... you would [self.timer invalidate]; self.timer = nil;.

于 2013-07-15T13:42:52.250 回答
0

I would personally just use performSelector:withObject:afterDelay: It would look something like this:

// Action on button press
- (IBAction)myActionButtonPress:(id)sender {
    // setup the selector to fire in 10 seconds
    [self performSelector:@selector(showAlert) withObject:nil afterDelay:10.0];
}

// the selector to be called
- (void)showAlert {
    // display alert
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                                    message:@"This alert displayed 10 seconds after button press"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

this would activate your action method after 10 seconds.

于 2013-07-15T13:43:16.543 回答
0

try this:

-(IBAction)btnDoneClick:(id)sender {
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(buttonactionalert) userInfo:nil repeats:NO];
}

-(void) buttonactionalert {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                            message:[NSString stringWithFormat:@"%d", hi]
                                               delegate:hi
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles: nil];
    [alert show];
}
于 2013-07-15T13:44:04.973 回答