お世話になります。
Swiftの関数について質問があります。
現在、Swift Docsを見ながらswiftの学習をしています。
その中で
https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/uikit/002-uibuttondebotanwo-biao-shi
// イベントを追加する. myButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside) // ボタンをViewに追加する. self.view.addSubview(myButton)
}
/*
ボタンのアクション時に設定したメソッド.
*/
internal func onClickMyButton(sender: UIButton){
print("onClickMyButton:")
}
この中のmyButton.addTargetの第二引数であるactionに、closureなどで作った関数をあてることはできないのでしょうか。
例えば、
let mySelect = {(sender: UIButton)->Void in print("onClickMyButton")}
myButton.addTarget(self,action: mySelect ~以下省略
としたいのです。ですが、このままだとエラーがでます
Cannot convert value of type "(UIButton) -> Void to expected argument type "Selector"
このように書くことはswiftで可能なのでしょうか
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/20 09:19