我试过了
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) 通过addTarget传递参数的新Xcode 7.3通常适用于我,但在这种情况下,它会在标题中抛出错误.有任何想法吗?当我尝试将其更改为@objc时,它会抛出另一个
谢谢!
cell.commentButton.addTarget(self, action: #selector(FeedViewController.didTapCommentButton(_:)), forControlEvents: UIControlEvents.TouchUpInside)
Run Code Online (Sandbox Code Playgroud)
它正在调用的选择器
func didTapCommentButton(post: Post) {
}
Run Code Online (Sandbox Code Playgroud) 如何设置Swift中自定义UIBarButtonItem的操作?
以下代码成功将按钮放在导航栏中:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:nil)
self.navigationItem.rightBarButtonItem = b
Run Code Online (Sandbox Code Playgroud)
现在,我想func sayHello() { println("Hello") }在触摸按钮时打电话.到目前为止我的努力:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:sayHello:)
// also with `sayHello` `sayHello()`, and `sayHello():`
Run Code Online (Sandbox Code Playgroud)
和..
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(sayHello:))
// also with `sayHello` `sayHello()`, and `sayHello():`
Run Code Online (Sandbox Code Playgroud)
和..
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(self.sayHello:))
// also with `self.sayHello` `self.sayHello()`, and `self.sayHello():`
Run Code Online (Sandbox Code Playgroud)
请注意,它sayHello()出现在intellisense中,但不起作用.
谢谢你的帮助.
编辑:对于后代,以下工作: …
我是swift的新手,我看到一些方法,如:
@objc private func doubleTapGestureRecognized(recognizer: UITapGestureRecognizer)
Run Code Online (Sandbox Code Playgroud)
我想知道什么时候使用@objc?我读了一些文档,但是他们说当你希望它在Objective-C中可以调用时,你应该添加@objc标志
但是,这是一个快速的私有函数,@ obj做了什么?
[ 注意这个问题最初是在Swift 2.2下制定的.它已针对Swift 4进行了修订,涉及两个重要的语言更改:第一个方法参数external不再自动被抑制,并且选择器必须显式地暴露给Objective-C.
假设我的课程中有这两种方法:
@objc func test() {}
@objc func test(_ sender:AnyObject?) {}
Run Code Online (Sandbox Code Playgroud)
现在我想使用Swift 2.2的新#selector语法来创建一个与这些方法中的第一个相对应的选择器func test().我该怎么做?当我尝试这个:
let selector = #selector(test) // error
Run Code Online (Sandbox Code Playgroud)
......我得到一个错误,"模糊地使用test()." 但如果我这样说:
let selector = #selector(test(_:)) // ok, but...
Run Code Online (Sandbox Code Playgroud)
......错误消失了,但我现在指的是错误的方法,一个带参数的方法.我想引用没有任何参数的那个.我该怎么做?
[注意:这个例子不是人为的.NSObject有Objective-C copy和copy:实例方法,Swift copy()和copy(sender:AnyObject?); 所以问题很容易在现实生活中出现.]
我正在将我的项目语法切换到Swift 2.2(xCode帮助我自动完成); 但是,我不明白新的#selector()语法.
举个例子:
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self,
selector: #selector(MyVC.timerCalled(_:)), //new selector syntax!
userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)
这有选择器 #selector(MyVC.timerCalled(_:))
这_:意味着什么?你能在这个选择器中添加其他变量吗?说,#MyVC.timerCalled(_:whateverVar).
关于这种语法的不同之处的一般信息与早期版本的Swift中基于字符串的实现相反,我们非常感激.
在大多数情况下,Swift在类型安全方面是对Objective-C的巨大改进.一个明显的例外是选择器.在Objective-C中,使用表达式@selector(notARealSelector:)会给出编译器警告.Swift等价物Selector("notARealSelector:")将始终编译但在运行时将失败.
是否有一种在Swift中使用选择器的类型安全方法,所以我可以使用需要它们的Objective-C API?
我的NSNotification应用程序中有很多观察者,并希望进行某种编译时检查,我不会在我的选择器中输入拼写错误.
编辑:具体用例是NSNotificationCenter.addObserver.
我正在尝试将此代码更新为swift 3:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)`
Run Code Online (Sandbox Code Playgroud)
到目前为止,我刚刚尝试了编译器给出的自动更正.这导致代码如下:
let notificationCenter = NotificationCenter.default()
notificationCenter.addObserver(self, selector: Selector(("keyboardWillShow:")), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
notificationCenter.addObserver(self, selector: Selector(("keyboardWillHide:")), name: NSNotification.Name.UIKeyboardWillHide, object: nil)`
Run Code Online (Sandbox Code Playgroud)
不幸的是,这并没有让我走得太远,导致额外的错误.
有人解决了吗?
请注意,我只是在尝试编写通知.我还没(尝试)修复通知功能..谢谢
对不起,这些问题
我在swift中有4个关于Selector的问题.
第一个问题
我想知道在swift中使用selector的正确方法是什么
closeBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop, target: self, action: Selector("closeBarButtonItemClicked:"));
Run Code Online (Sandbox Code Playgroud)
VS
closeBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop, target: self, action: "closeBarButtonItemClicked:");
Run Code Online (Sandbox Code Playgroud)
我们应该立即使用Selector("methodName:")或"methodName:"吗?
两种方式都有效但哪一种方法正确?
第二个问题
我们如何在Swift中使用参数调用函数?假设我想调用这样的函数
func methodName(parameterOne : String, parameterTwo: String)
Run Code Online (Sandbox Code Playgroud)
第三个问题
我们如何在swift中使用Selector调用类型方法?甚至可能吗?
class SomeClass {
class func someTypeMethod() {
// type method implementation goes here
}
}
Run Code Online (Sandbox Code Playgroud)
第四个问题
在Selector中函数名后面的冒号的目的是什么?
这里是Swift和iOS开发的超级新手.
我正在按照本教程关于在单个视图iOS应用程序中实现自定义控件.这是一个Swift 2教程,但到目前为止,我正在将所有内容转换为3(我使用XCode 8 Beta).
我有一个自定义类,RatingControl连接到View故事板中的一个.
在类的构造函数中,我创建了一个按钮:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.red()
Run Code Online (Sandbox Code Playgroud)
然后,我尝试为按钮分配一个动作.教程说我应该这样做:
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)),
for: .touchDown)
Run Code Online (Sandbox Code Playgroud)
然后在同一个RatingControl类中创建方法:
func ratingButtonTapped(button: UIButton) {
print("Button pressed ")
}
Run Code Online (Sandbox Code Playgroud)
但是当我编译时,它会抱怨:
类型"RatingControl"没有成员"ratingButtonTapped"
我已经100%确定函数在那里,在类中,并正确命名.完整来源
有什么明显的东西我不见了吗?
我尝试了什么:
@objc根据这个答案添加到类定义中(但对于仅限Swift的东西,这似乎很奇怪,不是吗?)
ratingButtonTapped()明确制作public(但看起来不应该是必要的)
摆弄字符串而不是选择器,button.addTarget(self, action: "RatingControl.ratingButtonTapped", for: .touchDown)还有更多,但这只会在以后崩溃.