Home>

It is the second day after starting swift, xcode and programming.
I started studying yesterday and made a simple fortune application while watching the video link below.
(This video was used as a reference.
https://www.youtube.com/watch?v=zucSRZcoPM0&t=201s)

With this application,
I'm trying to make an app that "presses 3 types of buttons to randomly display staples, main dishes, and side dishes".
Each button corresponds to each label (main food, main dish, side dish).

Therefore, in addition to these three types of buttons, I would like to install another "Change all three types of results" button.
① Staple food button The label of staple food changes
② Main dish button The label of the main dish changes
③ side dish button The side dish label changes
④All buttons ①②③ are executed at the same time with one tap

In order to create ④ above, we created a "change all" button and linked the buttons ①②③ to the button ④.
What code should I write in this case?
Also, if you have any reference pages or samples, please let me know.

Tried

Code

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var taitoru: UILabel!
@IBOutlet weak var syusyoku: UILabel!
@IBAction func syusyoku01 (_ sender: Any) {
let num = arc4random_uniform (31)
var result01 ="staple food result"

switch num {
case 0:
result01 ="curry"
// I will omit
case 30:
result01 ="steak"
default:
break
}
syusyoku.text = result01
}

@IBOutlet weak var hukusai: UILabel!
@IBAction func hukusai01 (_ sender: Any) {
let num = arc4random_uniform (31)
var result02 ="side dish result"

switch num {
case 01:
result02 ="Potato salad"
// I will omit
case 30:
result02 ="Infinite peppers"
default:
break
}
hukusai.text = result02

}

@IBOutlet weak var syusai: UILabel!
@IBAction func syusai01 (_ sender: Any) {
let num = arc4random_uniform (31)
var result03 ="Main dish result"

switch num {
case 01:
result03 ="Mapo Tofu"
// I will omit
case 30:
result03 ="Samurai Meunier"
default:
break
}
syusai.text = result03

}

@IBAction func ALL (_ sender: Any) {

// I want to make the above three buttons "syusyoku01", "hukusai01", and "syusai01" work simultaneously by pressing this "ALL" button.
I wasn't able to find a similar problem or not.
It seems that both the text and the code are difficult to understand.

}

}

Supplemental information (FW/tool version etc.)

xcode Version 9.4.1 (9F2000)

  • Answer # 1

    I ’m sorry, I ’m sorry.

    Make button content a function

    @ IBAction func hukusai01 (_ sender: Any) {
        // execute the function
        hukusaiBtn ()
    }
    func hukusaiBtn () {
        // Process button content
    }
    // All buttons
    @IBAction func ALL (_ sender: Any) {
        hukusaiBtn ()
        syusai ()
        // etc
    }

    Execute IBAction from code