output練習の第2回目です。

 

今回も講義で作ったアプリに、少し改良してみました。

 

講義では、ViewControllerの『+』と『-』押すと、LabelのTextColorが変わるという物です。

 

この講義で重要なのが、『if文』の基礎を覚える事です。

 

アプリ自体は、とても簡素でコードも短いです。

 

このアプリに、簡単な変更を加えてみました。

 

具体的には、Labelの数を2つにしました。

 

それと、ボタンに『Reset』ボタンを追加しました。

 

あと、前回の時は『if文』に、直接実行文を記入しました。

 

しかし、今回は『func』を使って、別にコード文を作って、それを『if文』に入れてみました。

 

今回は、outputのLevel1のPart2なので、こんなところですかね。

 

 

 

 

 

 

import UIKit

 

class ViewController: UIViewController {

 

    @IBOutlet weak var countLabel: UILabel!

    

    @IBOutlet weak var stringLabel: UILabel!

    

    var count = 0

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

 

    @IBAction func plus(_ sender: Any) {

        count = count + 1

        countLabel.text = String(count)

        

        if count >= 5 {

            plusText()

        }

        

        if count >= 10 {

            plusText2()

        }

        

    }

    

    @IBAction func minus(_ sender: Any) {

        count = count - 1

        countLabel.text = String(count)

        

        if count <= 5 {

            minusText()

        }

        

        if count <= 0 {

            minusText2()

        }

        

    }

    

    @IBAction func reset(_ sender: Any) {

        count = 0

        countLabel.text = String(count)

        

        if count == 0 {

            resetText()

        }

 

    }

    

    func plusText(){

        countLabel.textColor = .green

        stringLabel.textColor = .green

        stringLabel.text = "Swift"

    }

    

    func plusText2(){

        countLabel.textColor = .yellow

        stringLabel.textColor = .yellow

        stringLabel.text = "iPhone 11"

    }

    

    func minusText(){

        countLabel.textColor = .red

        stringLabel.textColor = .red

        stringLabel.text = "ios アプリ"

    }

    

    func minusText2(){

        countLabel.textColor = .blue

        stringLabel.textColor = .blue

        stringLabel.text = "プログラミング"

    }

    

    func resetText(){

        countLabel.textColor = .white

        stringLabel.textColor = .white

        stringLabel.text = "Count"

    }

    

}