简体   繁体   中英

clear the data in urtext field after 3 second when press button (action)

I have 4 UITextField for enter the number in each text field. And I have one button action . So when i press my button after some 3 second my all data in all UITextField field should clear automatically.

I know : textfield1.text = ""

But I don't know how to use delay and clear my data in my all UITextField field. Please help me out. I am using swift 2.2

Thanks

您只需在3秒后设置一个NSTimer日程安排,然后在操作中清除文本即可。

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    textfield1.text = ""
}

Step 1:

@interface ViewController ()<UITextFieldDelegate>

    @property (strong, nonatomic) NSTimer *timer;

@end

step 2 : on Button Click action

if (self.timer) {
    [self.timer invalidate];
    self.timer = nil;

}
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(clearTextField:) userInfo:nil repeats:NO];

Step 3 :

- (void) clearTextField :(NSTimer *) timer {

    // Clear textfield
    [self.timer invalidate];
    self.timer = nil;
}

you have many option:

1)

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    textfield1.text = ""
}

2) NSTimer

3) performSelector:withObject:afterDelay:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM