On the use of Nstimer in iOS development

Source: Internet
Author: User

This Sunday, my college classmates married. Their wedding is held in Zhengzhou. He is not Zhengzhou's, his girlfriend is not Zhengzhou. But they met in Zhengzhou in love, eventually came together, so they chose to hold a wedding in Zhengzhou.

In the last few years, everyone is busy and seldom have a chance to meet each other. But often telephone contact, mutual understanding of the situation.

As brothers, I am happy for them and bless them. I will also go to the scene, it is estimated that I have been in the development of Zhengzhou's train.

I like to go out of the feeling, like the car, go to all places, even if it is passing. The farther away you feel, the closer you spin. resignation to travel may soon be achieved.

Recently I am changing a bug, on a uiviewcontroller there is a timer, in the execution of Carousel Pictures. Push to another uiviewcontroller, pause the timer (Nstimer), and when the pop comes back, turn on the timer (Nstimer). This bug is: push to another Uiviewcontroller tentative timer (nstimer), sometimes it works, sometimes it doesn't work.

At first I thought there was a problem with the run loop running in Nstimer, and I found out that it was something else that caused the bug. But just right, let's have 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 is run by default mode, and Defaults mode almost includes all input sources (except Nsconnection) Nsdefaultrunloopmode mode.

The Actiontimer method is called once every 1s. Nstimer is not very simple to use. This is a relatively elementary application of Nstimer.

Nstimer fails when the main interface is sliding

What does it mean that the main interface is being slid? This means that the main interface has UITableView or uiscrollview, sliding uitableview or Uiscrollview. At this time the Nstimer failed.

Let's write a demo, start the timer on a UITableView uiviewcontroller, add 1 for each 1s number, and display the number on the 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);}

About the creation of UITableView and Uilabel I omitted. Detailed code can be downloaded here: Iosstrongdemo,iosstrongdemo I'll keep updating, and we'll get a star on GitHub.

This way, when the user drags the UITableView in Uitrackingrunloopmode, the Nstimer fails and cannot fire. The number on the Self.label cannot be updated.

Modify Nstimer's Run loop

The workaround is to add it to Uitrackingrunloopmode mode or Nsrunloopcommonmodes mode.

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

Or

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

Nsrunloopcommonmodes: is a collection of patterns that, when bound to a collection of events, is the equivalent of binding to each pattern within the collection.

Code:: Iosstrongdemo

?

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

On the use of Nstimer in iOS development

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.