読み込み中Apples swift(iOS)のドキュメントですが、Swift 2用に書かれています。私はSwift 3を使用します。プログラムでボタンを追加したいのですが、変更があり、それを修正する。ここSwift3:コード付きのボタンを追加する
コードありスイフト2例のためのものである:
import UIKit
class RatingControl: UIView {
// MARK: Initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Buttons
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.red()
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)
addSubview(button)
}
override func intrinsicContentSize() -> CGSize {
return CGSize(width: 240, height: 44)
}
// MARK: Button Action
func ratingButtonTapped(button: UIButton){
print("Button pressed")
}
}
"FIX-それはエラーがセレクタにこのされ示された後、私が作った唯一の変更:
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchDown)
この"ボタンが押された"と印刷されるはずですが、そうではありません。どんな助け?
何あなたRatingControl.ratingButtonTapped(ボタン:)方法について?それはその実装に依存します。 –
私が書いたすべてがAppleの例であるため、問題があるかどうかは分かりません...リンクは次のとおりです:tinyurl.com/q5oouqz –
@OnurTunaセレクタはそれを参照するだけです。実装に依存しないでください。 – Gerald