标签: nstimer

Swift中的@selector()?

我正在尝试创建一个NSTimerin Swift但我遇到了一些麻烦.

NSTimer(timeInterval: 1, target: self, selector: test(), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

test() 是同一个类中的函数.


我在编辑器中收到错误:

找不到接受提供的参数的'init'的重载

当我改变selector: test()selector: nil错误消失.

我试过了:

  • selector: test()
  • selector: test
  • selector: Selector(test())

但没有任何作用,我在参考文献中找不到解决方案.

selector nstimer swift

647
推荐指数
14
解决办法
30万
查看次数

我如何使用NSTimer?

我该如何使用NSTimer?谁能给我一步一步的指示?

cocoa objective-c nstimer

344
推荐指数
5
解决办法
51万
查看次数

如何在Swift中使用NSTimer?

我试过了

var timer = NSTimer()
timer(timeInterval: 0.01, target: self, selector: update, userInfo: nil, repeats: false)
Run Code Online (Sandbox Code Playgroud)

但是,我说错了

'(timeInterval: $T1, target: ViewController, selector: () -> (), userInfo: NilType, repeats: Bool) -> $T6' is not identical to 'NSTimer'
Run Code Online (Sandbox Code Playgroud)

nstimer nstimeinterval ios swift

246
推荐指数
11
解决办法
30万
查看次数

如何每X秒运行一次方法

我正在开发一个Android 2.3.3应用程序,我需要每隔X秒运行一个方法.

在iOS中,我有NSTimer,但在Android中我不知道该使用什么.

有人推荐我Handler ; 另一个推荐我AlarmManager,但我不知道哪种方法更适合NSTimer.

这是我想在Android中实现的代码:

timer2 = [
    NSTimer scheduledTimerWithTimeInterval:(1.0f/20.0f)
    target:self
    selector:@selector(loopTask)
    userInfo:nil
    repeats:YES
];

timer1 = [
    NSTimer scheduledTimerWithTimeInterval:(1.0f/4.0f)
    target:self
    selector:@selector(isFree)
    userInfo:nil
    repeats:YES
];
Run Code Online (Sandbox Code Playgroud)

我需要一些像NSTimer一样的东西.

你推荐我什么?

android timer nstimer

102
推荐指数
6
解决办法
14万
查看次数

UIScrollView暂停NSTimer直到滚动完成

当一个UIScrollView(或其派生类)滚动时,似乎所有NSTimers正在运行的都会暂停,直到滚动完成.

有办法解决这个问题吗?主题?优先级设置?什么?

uiscrollview nstimer ios

82
推荐指数
6
解决办法
2万
查看次数

检测应用程序何时进入我的视图的背景时,最好的方法是什么?

我有一个视图控制器,它使用一个NSTimer来执行一些代码.

什么是检测应用程序何时进入后台的最佳方法,以便我可以暂停计时器?

nstimer ios

71
推荐指数
4
解决办法
7万
查看次数

确定UIView是否对用户可见?

是否可以确定我UIView的用户是否可见?

我的视图被subview多次添加到一个Tab Bar Controller.

此视图的每个实例都有一个NSTimer更新视图的实例.

但是,我不想更新用户不可见的视图.

这可能吗?

谢谢

iphone visibility uitabbarcontroller nstimer uiview

69
推荐指数
6
解决办法
7万
查看次数

如何在后台线程上创建NSTimer?

我有一项需要每1秒执行一次的任务.目前我每隔1秒就有一次NSTimer重复射击.如何在后台线程(非UI线程)中触发计时器?

我可以在主线程上使用NSTimer激发然后使用NSBlockOperation来调度后台线程,但我想知道是否有更有效的方法来执行此操作.

cocoa objective-c nstimer nsrunloop nsblockoperation

66
推荐指数
6
解决办法
6万
查看次数

将参数传递给NSTimer调用的方法

如何将参数传递给NSTimer调用的方法?我的计时器看起来像这样:

[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(updateBusLocation) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)

我希望能够将字符串传递给方法updateBusLocation.另外,我应该在哪里定义updateBusLocation方法?在我创建计时器的同一个.m文件中?

编辑:

其实我还有问题.我收到错误消息:

由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' * - [MapKitDisplayViewController updateBusLocation]:无法识别的选择器发送到实例0x4623600'

这是我的代码:

- (IBAction) showBus {

//do something

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateBusLocation) userInfo:txtFieldData repeats:YES];
[txtFieldData release];
 }


 - (void) updateBusLocation:(NSTimer*)theTimer
 {
      NSLog(@"timer method was called");
      NSString *txtFieldData = [[NSString alloc] initWithString:(NSString*)[theTimer userInfo]];
if(txtFieldData == busNum.text) {
    //do something else
    }
    }
Run Code Online (Sandbox Code Playgroud)

编辑#2:没关系,您的示例代码可以正常工作,感谢您的帮助.

cocoa-touch objective-c selector nstimer

60
推荐指数
2
解决办法
4万
查看次数

在目标c中以自定义格式获取字符串中的当前时间

我想在字符串中使用以下格式的当前时间.

dd-mm-yyyy HH:MM

怎么样?

iphone datetime objective-c nstimer

56
推荐指数
3
解决办法
8万
查看次数