Home > Blockchain >  Using multiple toggles I'would like to regroup all toggles in one action
Using multiple toggles I'would like to regroup all toggles in one action

Time:01-31

I'm trying to create an array composed of multiple tools. The array changes depending on which tools are carried. Expecting a long list of tools I would like to avoid having to write an action for each single element to add or remove from the array. ( I managed this part as you'll see below)

Instead I'd like to use the name of the toggle switch and apply the same action to all the toggles on the screen.

I came up with this inelegant method:

import UIKit

class mainVC: UIViewController {
    
    
    var myEquippement: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        }

    
    
    @IBAction func shovelSwitch(_ sender: UISwitch) {
                
        if sender.isOn{
            myEquippement.append("Shovel")
        }
        else {
            let subjectIndex = myEquippement.firstIndex(of: "Shovel")
            myEquippement.remove(at: subjectIndex!)
        }
        
    }
    
    
    @IBAction func hammerSwitch(_ sender: UISwitch) {
        
        if sender.isOn{
            myEquippement.append("Hammer")
        }
        else {
            let subjectIndex = myEquippement.firstIndex(of: "Hammer")
            myEquippement.remove(at: subjectIndex!)
        }
    }
    
    
    @IBAction func screwdriverSwitch(_ sender: UISwitch) {
        if sender.isOn{
            myEquippement.append("Screwdriver")
        }
        else {
            let subjectIndex = myEquippement.firstIndex(of: "Screwdriver")
            myEquippement.remove(at: subjectIndex!)
        }
    }
    
    
    @IBAction func sawSwitch(_ sender: UISwitch) {
        if sender.isOn{
            myEquippement.append("Saw")
        }
        else {
            let subjectIndex = myEquippement.firstIndex(of: "Saw")
            myEquippement.remove(at: subjectIndex!)
        }
    }
}

could you point me please to a better way of doing it.

I thought of using something like this:

@IBAction func toggleSwitched(_ sender: UISwitch) {
        if sender.isOn{
            myEquippement.append(sender.title!)
        }
        else {
            
            let subjectIndex = myEquippement.firstIndex(of: sender.title!)
            myEquippement.remove(at: subjectIndex!)
            
        }
        
    }

but sender.title always returns a nil value, the force unwrapping crashes the app.

Thanks in advance for your help.

CodePudding user response:

One solution would be to have a master array of tool names. When creating a switch, assign its tag property to the corresponding index within that array.

Then in your single switch handler you can use the sender's tag to get the name of the tool from the master list of tool names. That will give you the title for the switch. Then your logic for adding/removing the title from myEquipment can be used.

  • Related