草庐IT

Selector

全部标签

xcode - 更新到 Xcode 7.3/Swift 2.2 后计时器停止工作

更新到Swift2.2和Xcode7.3后,我的重复NSTimer停止重复了。lettimer=NSTimer.scheduledTimerWithTimeInterval(0.4,target:self,selector:#selector(rotate),userInfo:nil,repeats:true)timer.fire()选择器触发一次,然后直到窗口关闭或最小化后才会再次触发。还有其他人吗?有什么建议吗? 最佳答案 计时器需要始终在同一个线程中进行调度或失效,您可能是在异步block中调用它?尝试在主队列中安排它:dis

ios - 使用选择器为 UItapgestureRecognizer 传递额外参数

我有两个标签,Label1和Label2。我想制作一个函数,通过为两个标签创建UITTapRecognizer并使用传递参数的选择器调用相同的函数来打印出哪个标签被点击。下面是实现它的漫长方法,它很困惑但有效。如果我知道如何将参数(Int)传递给选择器,它会更清晰。lettopCommentLbl1Tap=UITapGestureRecognizer(target:self,action:#selector(DiscoverCell().doubleTapTopComment1))topCommentLbl1Tap.numberOfTapsRequired=2topCommentLbl

swift - selector为静态方法时如何设置NotificationCenter的观察者

funcaddObserver(_observer:Any,selectoraSelector:Selector,nameaName:NSNotification.Name?,objectanObject:Any?)此函数需要观察者是某个对象,但同时将静态方法设置为选择器。Thisanswerexplainshowtosetselectorandobserverwhentheselectorisaninstancemethod. 最佳答案 我们需要设置YourClass.self为观察者。这样——NotificationCenter

swift - 无法识别的选择器发送到 AppDelegate 中的实例

我试图让一个方法每5秒调用一次,无论应用程序是否打开或在后台运行,所以在我的AppDelegate中,我认为有一个调用方法的计时器是个好主意每5秒:varhelloWorldTimer=Timer.scheduledTimer(timeInterval:5.0,target:self,selector:#selector(sayHello),userInfo:nil,repeats:true)@objcfuncsayHello(){print("helloWorld")}但是,我得到这个错误:NSInvalidArgumentException',reason:'-[_SwiftVal

Swift 将字符串转换为函数名 (#selector)

我创建了一个接收字符串的函数。该字符串必须转换为函数名称,然后作为#selector参数传递。我试过了,但没用:funcsetFunctionWithTap(functionName:String){lettap=UITapGestureRecognizer(target:self,action:#selector(functionName))...}你能告诉我这是否可能吗? 最佳答案 使用选择器initializerthatacceptsastring相反:lettap=UITapGestureRecognizer(target:

ios - UISwipeGesture 'unrecognized selector sent to instance'

如果有人能帮我解决这个问题,那就太好了!我花了几个小时试图修复,但没有成功错误...2016-06-2320:30:43.341057ScalingRings[408:38903][DYMTLInitPlatform]platforminitializationsuccessful2016-06-2320:30:43.750822ScalingRings[408:38776]MetalGPUFrameCaptureEnabled2016-06-2320:30:43.751531ScalingRings[408:38776]MetalAPIValidationEnabled2016-06

类中的 Swift 3 计时器未触发

我有一个类:classGameManager{...在其中我有这个功能:funcstartGame(){msgTimer=Timer(timeInterval:0.5,target:self,selector:#selector(typeMessage(_:)),userInfo:nil,repeats:true)}及其调用的选择器:@objcfunctypeMessage(_sender:Timer){iftextCount>strInitText.characters.count{letstrThisChar=strInitText[strInitText.index(strIni

macos - [NSSearchField 对象] : unrecognized selector sent to instance

我正在一个简单的Mac应用程序中测试Swift。我在Storyboard中得到了一个NSToolbar并在里面画了一个NSSearchfield。NSSearchfiled连接到第一响应者的方法controlTextDidChange(第一响应者是我添加了NSTextFieldDelegate的ViewController)。这是方法:@IBActionoverridefunccontrolTextDidChange(obj:NSNotification!){println("searching...")println(obj.object.stringValue)}每次搜索新角色时都

ios - 如何在运行时通过 Selector 在 Swift 中调用泛型函数?

我正在尝试像这样在运行时为UI事件注册回调。funcobserveEvent(event:UIControlEvent){self.addTarget(self,action:"eventFired:",forControlEvents:event)}funceventFired(sender:T){print("eventfired!")}它崩溃并说它在运行时找不到eventFired:。你能帮忙解决这个问题吗? 最佳答案 您不能使用通用函数作为选择器字符串文字的目标。为什么?选择器不包含有关其目标参数类型的信息,仅包含:选择器目

swift - 想了解关系

我是一名在Xcode8中学习Swift3的初学者,我正在构建一个名为“Eggtimer”的基本应用程序。代码写在下面,我不明白timerlabel.text是如何链接到计时器的,即使我没有在它们之间设置任何连接。在星形//*旁边,我们还可以写}else{timer.invalidate()并且labeltimer.text很好地停止减少,怎么可能发生?timer属性中的selector是什么意思?抱歉我的英语不好,感谢您的回答。classViewController:UIViewController{vartimer=Timer()vartime=210funcdecreasetime