LoginSignup
0
2

More than 3 years have passed since last update.

Swift UIButtonの使い方

Posted at

今回はUIButtonの使い方について書いていきます

storyboardからの接続を完了した後のコードです

ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func tappedButton(_ sender: Any) {
    }

}

buttonの接続はoutletではなく、actionにします

tappedbutton関数の中に処理を書いていきます
今回はlabelのtextを変更しましょう

ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func tappedButton(_ sender: Any) {
        label.text = "HelloWorld"
    }

}

これでlabeltextHelloWolrdに変更されます

このままではtextHelloWorldのままで終わってしまい面白くないので
tappedButton関数の中身を少し変更しましょう


@IBAction func tappedButton(_ sender: Any) {
        isValue.toggle()
        if isValue {
            label.text = "HelloWorld"
        } else {
            label.text = String(isValue)
        }
}

こうするとisValuetrueの時labelHelloWolrdfalseの時にfalseが表示されます

今回はこれで終わります

0
2
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
2