Usage of NSTimer in iOS development

Source: Internet
Author: User

Usage of NSTimer in iOS development

My college student got married this Sunday. Their wedding was held in Zhengzhou. He is not from Zhengzhou, and his girlfriend is not from Zhengzhou. But they fell in love with each other in Zhengzhou and finally came together, so they chose to hold a wedding in Zhengzhou.

Everyone has been busy over the past few years, and there are very few opportunities to meet each other. However, we often call each other to learn about the situation.

As a brother, I feel happy for them and wish them good luck. I will also attend the event. It is estimated that I have now taken the bullet train to develop Zhengzhou.

I like the feeling of going out. I like taking a bus and going to different places, even if it is passing.Feeling farther and closer, the closer the centrifugation.Leaving for a trip may soon be possible.

Recently, I am modifying a bug. There is a timer on a UIViewController and the carousel picture is being executed. When pushing to another UIViewController, the timer (NSTimer) is paused; when the pop is back, the timer (NSTimer) is enabled ). This bug is: push to another UIViewController tentative timer (NSTimer). Sometimes it works, sometimes it does not.

At first, I thought there was a problem with the run loop run by NSTimer. Finally, I found that this bug was caused elsewhere. But let's take a look at NSTimer.

Create a timer (NSTimer)
- (void)viewDidLoad {    [super viewDidLoad];    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(actionTimer:) userInfo:nil repeats:YES];}- (void)actionTimer:(NSTimer *)timer{}

NSTimer runs in the default mode by default. The default mode includes almost all input sources (except NSConnection) NSDefaultRunLoopMode.

The actionTimer method is called every 1 s. NSTimer is easy to use. This is a relatively elementary application of NSTimer.

NSTimer becomes invalid when the main interface is sliding

What does sliding the main interface mean? That is to say, the main interface has UITableView or UIScrollView, sliding UITableView or UIScrollView. In this case, NSTimer becomes invalid.

Let's write a demo. Start the timer on a UIViewController with UITableView, add 1 to every 1 s number, and display this number on UILabel.

- (void)viewDidLoad {    [super viewDidLoad];    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(actionTimer:) userInfo:nil repeats:YES];}- (void)actionTimer:(NSTimer *)timer{    self.number++;    self.label.text = [NSString stringWithFormat:@%d,self.number];    NSLog(@%d,self.number);}

The creation of UITableView and UILabel is saved. For detailed code, click here to download: iOSStrongDemo, iOSStrongDemo I will continue to update, and everyone will star it on github.

In this way, when the user is dragging the UITableView in UITrackingRunLoopMode, NSTimer becomes invalid and cannot be fired. The numbers on self. label cannot be updated.

Modify the run loop of NSTimer

The solution is to add it to the UITrackingRunLoopMode mode or the nsunloopcommonmodes mode.

[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];

Or

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

Nsunloopcommonmodes: A mode set. When an event source is bound to this mode set, it is equivalent to binding to each mode in the set.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.