0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Swift button action と function の 連結

Last updated at Posted at 2021-11-24

最初にUIButtonのイニシャライゼーションの指定から。

var btn1 = UIButton()

これを、viewDidLoad(){} の中に書くと、画面がロードされたときのみ、{}内だけで有効なイニシャライズ=初期化。下記は、クラス全体で有効なイニシャライズ。

class ViewController: UIViewController {
var btn1 = UIButton()

  override func viewDidLoad(){
    btn1.frame = CGRect(x: 左端からの位置, y: 上からの位置, width: 幅, height: 高さ)
    btn1.setTitleColor(.gray, for: .normal)
    btn1.backgroundColor = .black
    btn1.textAlignment = .left
    btn1.addTarget(self, action: #selector(button1(button1;)), for: .touchUpInside)
    self.view.addSubview(btn1)
  }
  @objc button1(button1: UIButton){
    btn1.backgroundColor = .purple
    object()
  }
  func object(){
    // New event start.
  }  
}

このようにしてロードした直後のボタンの位置決めと、タップイベントの作成が開始され、ファンクション化されて連結されていく。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?