【发布时间】:2014-09-15 05:54:48
【问题描述】:
我有一个横幅数组并将其显示在我的自定义单元格中。当用户点击横幅时,我推送detailViewController 并打开应用内浏览器。
我每 5 秒更换一次横幅。为此,我使用NSTimer 来安排选择器调用。一切都很好......直到,用户点击横幅并从detailViewController回来。当用户回来时, NSTimer 的行为真的很奇怪。它在 5 秒后更改第一个横幅(按分配),然后在 1 秒后更改下一个横幅,依此类推。
她是我正在使用的代码:
#pragma mark - User Methods
-(void) resetBannerRotationTimer {
[self.bannerTimer invalidate];
self.bannerTimer = nil;
self.timeInterval = 5.0f;
self.bannerTimer = [NSTimer scheduledTimerWithTimeInterval:self.timeInterval target:self selector:@selector(rotateBanner) userInfo:nil repeats:YES];
self.timeInterval = self.bannerTimer.timeInterval;
[[NSRunLoop currentRunLoop] addTimer:self.bannerTimer forMode:NSRunLoopCommonModes];
}
旋转横幅:
-(void) rotateBanner {
BannerCell *bannerCell = (BannerCell *)[self.dealsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[bannerCell updateBanner];
}
在我的updateBanner 方法中,我正在处理UIPageControl 以更改页面。 (我认为不需要发布代码)。
我在viewWillAppear方法中调用resetBannerRotationTimer方法。
【问题讨论】:
-
您的
resetBannerRotationTimer被调用了多少次?我认为你应该使用scheduledTimerWithtimeInterval。那么你就不需要在runLoop中添加定时器了。 -
代码看起来不错。在 resetBannerRotationTimer 方法中保留断点。查看计时器对象是否有效。在调试器窗口中使用 po [NSRunLoop currentLoop] 打印内容并验证计时器对象的属性。看看那里有没有问题。
-
我试过
scheduledTimerWithtimeInterval,它在 5 秒和 3 秒后改变横幅。不过,时间间隔是 5 秒。 -
我也更新了我的
resetBannerRotationTimer方法。 -
得到了问题。当用户点击横幅时,我的计时器没有失效。它不应该造成问题,但它仍然存在。我在点击时将计时器设置为零并且它正在工作。
标签: ios nstimer uipagecontrol