迅速なiOSアプリケーションの作成では、親ビューコントローラの外でUIButtonプレスのイベントを処理する必要があったため、その責任を別のクラスに委譲する(非常に簡単な)プロトコルを作成しました:`AnyObject? 'に変換できません。
import UIKit
protocol MyButtonProtocol {
func buttonPressed(sender: UIButton)
}
しかし、そのプロトコルでUIButtonにaddTargetを実行しようとすると、私はこのエラーを受け取ります:Cannot convert value of type 'MyButtonProtocol' to expected argument type 'AnyObject?'
。 何かをAnyObject?
に変換することはできませんか?私のメインコードは以下の通りです:
import UIKit
class MyView: UIView {
var delegate: MyButtonProtocol
var button: UIButton
init(delegate: MyButtonProtocol) {
self.delegate = delegate
button = UIButton()
//... formatting ...
super.init(frame: CGRect())
button.addTarget(delegate, action: "buttonPressed:", forControlEvents: .TouchUpInside)
addSubview(button)
//... more formatting ...
}
}
ありがとうございます。
シンプル&完璧!! – iAnurag