我正在尝试创建一个NSTimer
in 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())
但没有任何作用,我在参考文献中找不到解决方案.
我试过了
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) 我正在开发一个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一样的东西.
你推荐我什么?
当一个UIScrollView
(或其派生类)滚动时,似乎所有NSTimers
正在运行的都会暂停,直到滚动完成.
有办法解决这个问题吗?主题?优先级设置?什么?
我有一个视图控制器,它使用一个NSTimer
来执行一些代码.
什么是检测应用程序何时进入后台的最佳方法,以便我可以暂停计时器?
是否可以确定我UIView
的用户是否可见?
我的视图被subview
多次添加到一个Tab Bar Controller
.
此视图的每个实例都有一个NSTimer
更新视图的实例.
但是,我不想更新用户不可见的视图.
这可能吗?
谢谢
我有一项需要每1秒执行一次的任务.目前我每隔1秒就有一次NSTimer重复射击.如何在后台线程(非UI线程)中触发计时器?
我可以在主线程上使用NSTimer激发然后使用NSBlockOperation来调度后台线程,但我想知道是否有更有效的方法来执行此操作.
如何将参数传递给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:没关系,您的示例代码可以正常工作,感谢您的帮助.
我想在字符串中使用以下格式的当前时间.
dd-mm-yyyy HH:MM
怎么样?
nstimer ×10
objective-c ×4
ios ×3
cocoa ×2
iphone ×2
selector ×2
swift ×2
android ×1
cocoa-touch ×1
datetime ×1
nsrunloop ×1
timer ×1
uiscrollview ×1
uiview ×1
visibility ×1