0
1

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

swift辞書「要素を非表示にする」

Last updated at Posted at 2021-02-21

お断り

この記事は自分専用メモなのですごい初歩的な事しか書いていませんがご了承下さい。
確認環境
Interface:Storyboard
Life Cycle:UIKit App Delegate
Language:Swift
◉Use Core Data
__○Host in CloudKit
◉Include Tests

この記事で出来る様になる事

ボタンが押された時に要素を非表示にする

実装

UIの実装

Image Viewを配置した後AutoLayoutのHorizontal Center in Container,Vertical Center in Containerにチックを付けて動かない様にします。次にImageで好きな写真を選択します。次にbuttonを配置してAutoLayoutのHorizontal Center in Containerにチェックを付けて動かない様にします。次にadd new constraintsのconstrain to marginsチェックを取ってから下の数値を120にして、最後にpushと名前を変えるこれで完成です。スクリーンショット 2021-02-21 13.59.47.png

コードの実装

最終的なコードはこちらです。

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet var Item: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    @IBAction func push() {
        Item.isHidden = !Item.isHidden
    }

}

**@IBOutlet var Item: UIImageView!**ではImageViewとこのコードを関連づける働きが有ります。
**@IBAction func push()**では関連づけしたボタンが押された時に実行される関数です。
Item.isHidden = !Item.isHiddenではItemを表示するか非表示にするかを決定する要素がbool型で管理されているので!で反転したbool値を代入して表示非表示を切り替えます。

関連付け

OutletsのitemをImageViewを関連づけて最後にReceived Actionsのpushはスクリーンショット 2021-02-21 8.23.27.png
この選択した物を選びます。
これで完成です。Videotogif.gif

細かいところ

特に無し

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?