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.

UIButton と Userdefalts のカウンター

Posted at

UIButtonはオブジェクト指向(ストーリーボード)と、文字式で書くことの二種類の設置方法があります。

@IBOutlet weak var btn1!

と override func viewDidLoad(){
の中でポジションを決める方法です。両方、UIButton.frameなどの位置どりが必要です。

var ii = Int()
var users = UserDefaults.standard
var label = UILabel()

override func viewDidLoad(){
    // ボタンの初期化    
    let btn = UIBUtton()
    // ボタンの位置決め。左から=x・上から=y・幅=width・高さ=height。
    btn.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
    btn.setTitle = ""
    btn.addTarget(self, action: #selector(btn(btn:)), for: .touchUpInSide)
    self.view.addSubview(btn)
    label.frame = CGRect(x: 100, y: 100, width: 140, height: 60)
    self.view.addSubview(label)
    ii = users.integer("k1")

}
@objc func btn(btn: UIButton){
    ii += 1
    label.text = String(ii)
}
override func viewDidDisAppear(){
    users.set(ii, for: "k1")
}

ボタンアクションと、カウンターをアクションごとにアップし、画面が消えると記憶する仕組みにしました。

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?