0
2

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 5 years have passed since last update.

ボタン入力で好きな数字を表示できるコード書きました 初心者なんでなんか間違っててもいじめないでね;;

Posted at

import UIKit

var counter:Int = 0

class ViewController: UIViewController {

@IBOutlet weak var num: UILabel!
@IBOutlet weak var button0: UIButton!
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!
@IBOutlet weak var button5: UIButton!
@IBOutlet weak var button6: UIButton!
@IBOutlet weak var button7: UIButton!
@IBOutlet weak var button8: UIButton!
@IBOutlet weak var button9: UIButton!
@IBOutlet weak var buttonReturn: UIButton!

@IBAction func button0(_ sender: Any) {
    num.text = String(counter * 10)
    counter = counter * 10
}
@IBAction func button1(_ sender: Any) {
    num.text = String(counter * 10 + 1)
    counter = counter * 10 + 1
}
@IBAction func button2(_ sender: Any) {
    num.text = String(counter * 10 + 2)
    counter = counter * 10 + 2
}
@IBAction func button3(_ sender: Any) {
    num.text = String(counter * 10 + 3)
    counter = counter * 10 + 3
}
@IBAction func button4(_ sender: Any) {
    num.text = String(counter * 10 + 4)
    counter = counter * 10 + 4
}
@IBAction func button5(_ sender: Any) {
    num.text = String(counter * 10 + 5)
    counter = counter * 10 + 5
}
@IBAction func button6(_ sender: Any) {
    num.text = String(counter * 10 + 6)
    counter = counter * 10 + 6
}
@IBAction func button7(_ sender: Any) {
    num.text = String(counter * 10 + 7)
    counter = counter * 10 + 7
}
@IBAction func button8(_ sender: Any) {
    num.text = String(counter * 10 + 8)
    counter = counter * 10 + 8
}
@IBAction func button9(_ sender: Any) {
    num.text = String(counter * 10 + 9)
    counter = counter * 10 + 9
}
@IBAction func buttonReturn(_ sender: Any) {
    num.text = String(counter / 10)
    counter = counter / 10
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    
    num.text = String(counter)
    button0.setTitle("0",for:UIControl.State.normal)
    button1.setTitle("1",for:UIControl.State.normal)
    button2.setTitle("2",for:UIControl.State.normal)
    button3.setTitle("3",for:UIControl.State.normal)
    button4.setTitle("4",for:UIControl.State.normal)
    button5.setTitle("5",for:UIControl.State.normal)
    button6.setTitle("6",for:UIControl.State.normal)
    button7.setTitle("7",for:UIControl.State.normal)
    button8.setTitle("8",for:UIControl.State.normal)
    button9.setTitle("9",for:UIControl.State.normal)
    buttonReturn.setTitle("←",for:UIControl.State.normal)
}

}

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?