How to refer #IBAction Func to several button outlets - swift

I have lots of buttons that when pressed run very similar code, im currently writing a function for each, is there a way to compact this to one function?
Heres some of the code:
#IBAction func b0(_ sender: UIButton) {
if pressedArray[0] && buttonsCanBePressed {
pressedArray[0] = false
b0.backgroundColor = notPressedColour
} else if buttonsCanBePressed {
b0.backgroundColor = pressedColour
pressedArray[0] = true
}
}
#IBAction func b1(_ sender: UIButton) {
if pressedArray[1] && buttonsCanBePressed {
pressedArray[1] = false
b1.backgroundColor = notPressedColour
} else if buttonsCanBePressed {
b1.backgroundColor = pressedColour
pressedArray[1] = true
}
}
#IBAction func b2(_ sender: UIButton) {
if pressedArray[2] && buttonsCanBePressed {
pressedArray[2] = false
b2.backgroundColor = notPressedColour
} else if buttonsCanBePressed {
b2.backgroundColor = pressedColour
pressedArray[2] = true
}
}

Yes, You can make only one IBAction function, and then drag the circle in the line number to all the buttons you want, and you can differentiate the buttons using sender.tag

Related

Print the result of button every time pressed

import UIKit
class ViewController: UIViewController {
let softTime = 5
let mediumTime = 7
let hardTime = 12
#IBAction func hardnessSelected(_ sender: UIButton) {
// print(sender.currentTitle)
let hardness = sender.currentTitle
/* I want print the of let if pressed first button or second ... */
The .currentTitle property is an optional and must be unwrapped. Try this:
#IBAction func hardnessSelected(_ sender: UIButton) {
if let buttonTitle = sender.title(for: .normal) {
print(buttonTitle)
}
}
// thank you guys I fount the answer
switch hardness {
case "Soft":
print(5)
case "Medium":
print(7)
case "Hard":
print(12)
default:
print("Error")
}
}
import UIKit
let softTime = 5
let mediumTime = 7
let hardTime = 12
class ViewController: UIViewController {
#IBAction func hardnessSelected(_ sender: UIButton) {
let hardness = sender.currentTitle
//control flow
if (hardness == "Soft") {
print(softTime)
}else if (hardness == "Medium") {
print(mediumTime)
}else if (hardness == "Hard") {
print(hardTime)
}
}
}
Common decision

go back and forth a string-array with two buttons

I want to show the first item on viewdidload() and then have the user go to the next item in the array and have the option to go back and disable the back button if it’s the first index and disable the button if its the last index. Here is my code
#IBAction func back(_ sender: Any) {
c -= 1
if c == thkrArray.count{
nextButton.isEnabled = false
}
if c == 0{
backButton.isEnabled = false
}
let thkr: String = thkrArray[c]
Text1.text = thkr
}
#IBAction func A2(_ sender: Any) {
c += 1
if c == thkrArray.count{
nextButton.isEnabled = false
}
if c == 0 {
backButton.isEnabled = false
}
if c > 0{
backButton.isEnabled = false
}
let thkr: String = thkrArray[c]
Text1.text = thkr
}
The easiest way to achieve this is to add an observer to c:
var c: Int = 0 {
didSet {
Text1.text = thkrArray[c]
backButton.isEnabled = c > 0
nextButton.isEnabled = c < thkrArray.count - 1
}
}
#IBAction func back(_ sender: Any) {
c -= 1
}
#IBAction func A2(_ sender: Any) { // isn't this `next`?
c += 1
}
override func viewDidLoad() {
super.viewDidLoad()
c = 0
}

How can I remove repetitive code in swift?

I have 2 sections and 6 lines of code that do the opposite things for an app that I have made for a project. The code was typed as it was taught to us, but in the project review I was told to get rid of the repetitive/like code. Can anyone help point me in the right direction with this? Thanks
#IBAction func stopRecording(_ sender: AnyObject) {
recordButton.isEnabled = true
stopRecordingButton.isEnabled = false
recordingLabel.text = "Tap to Record"
}
#IBAction func recordAudio(_ sender: AnyObject)
{
recordingLabel.text = "Recording in Progress"
stopRecordingButton.isEnabled = true
recordButton.isEnabled = false
}
You can also do this by didSet Observer.
var isRecording: Bool = false {
didSet {
recordButton.isEnabled = !isRecording
stopRecordingButton.isEnabled = isRecording
recordingLabel.text = isRecording ? "Recording in progress" : "Tap to Record"
}
}
And the actions become pretty simple like this.
#IBAction func stopRecording(_ sender: AnyObject) {
isRecording = false
}
#IBAction func recordAudio(_ sender: AnyObject) {
isRecording = true
}

If (Bools = true) not executing -Swift

Attempting to execute a modal segue when multiple different Bool variables are all true (activated true through IBAction button push), however, nothing is happening- here is how they are all setup-
UIViewController {
// INITIAL TO CHECK WHICH BUTTONS HAVE BEEN PUSHED //
var 1Check = Bool()
// Checks //
#IBAction func 1(_ sender: AnyObject) {
1Check = true
}
and here is the execution-
viewDidLoad() {
super.viewDidLoad()
MoveOn()
}
func MoveOn(){
if (1Check == true && 2Check == true ...) {
self.performSegue(withIdentifier: "NewScreen", sender: nil)
}
}
what am I missing? Thanks!
The call to MoveOn() needs to be in a place where it will be called every time one of those checked values changes:
UIViewController {
// INITIAL TO CHECK WHICH BUTTONS HAVE BEEN PUSHED //
var 1Check = Bool()
// Checks //
#IBAction func 1(_ sender: AnyObject) {
1Check = true
MoveOn()
}
viewDidLoad() {
super.viewDidLoad()
}
func MoveOn(){
if (1Check == true && 2Check == true ...) {
self.performSegue(withIdentifier: "NewScreen", sender: nil)
}
}
}

Swift: Missing Argument Error

Im having an issue here when trying to follow the first stanford lecture. I am assuming it is because I am using swift 2.... which was just recently released. (And the class is not) Below I show where I am getting errors. In the class the "enter" button on the calculator has the code #IBAction func enter() { but in mine it is #IBAction func enter(sender: UIButton) {
Is this something I did wrong when bringing the button into my code? Any ways to fix? Let me know if I can clarify anything.
import UIKit
class ViewController: UIViewController
{
#IBOutlet var display: UILabel!
var userIsInTheMiddleOfTypingANumber: Bool = false
#IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if userIsInTheMiddleOfTypingANumber {
display.text = display.text! + digit
} else {
display.text = digit
userIsInTheMiddleOfTypingANumber = true
}
}
#IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsInTheMiddleOfTypingANumber {
enter() // Here is my error: *Missing Argument for parameter on the line of code that says enter()
}
switch operation {
case "x":
if operandStack.count >= 2 {
displayValue = operandStack.removeLast() * operandStack.removeLast()
enter() // Here is my error: Again...
}
default: break
}
}
var operandStack = Array<Double>()
#IBAction func enter(sender: UIButton) {
userIsInTheMiddleOfTypingANumber = false
operandStack.append(displayValue)
print("operandStack = \(operandStack)")
}
var displayValue: Double {
get {
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set {
display.text = "\(newValue)"
userIsInTheMiddleOfTypingANumber = false
}
}
}
enter requires an input parameter but you didn't supply it with any. Replace it with enter(sender)
#IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsInTheMiddleOfTypingANumber {
enter(sender)
}
switch operation {
case "x":
if operandStack.count >= 2 {
displayValue = operandStack.removeLast() * operandStack.removeLast()
enter(sender)
}
default: break
}
}