Search code examples
swiftuipickerview

Create a warning based on UIPickerViewSelection


So here is what I am trying to do.

I am working on an app that allows the company I work for to see what account number they should use when making a purchase. There are certain locations where the costs should be split to different accounts based on location, but they are only for a certain few. I would like to issue a warning in the form of a popup, which I already have coded. My thought process is that I can use if/else statements to issue what I need, but I am unsure as to how to word it. It would supply a regular account number if the accounts need not be split, but then a warning statement if it did. The code is below. My problem is under the didSelectRow.

What's not actually shown is the PopUpVC I am using and the full list of array data because it's long.

Much thanks to this website as it has really helped me code my first project!

let account: [Account] = [Account(number: 0, description: "SELECT")]
//there is actually more data here but I am taking it out so it's shorter

let tasks: [Task] = [Task(number: 0, description: "SELECT")]
//there is actually more data here but I am taking it out so it's shorter

let locations: [Location] = [Location(number: 0, description: "SELECT")]
//there is actually more data here but I am taking it out so it's shorter

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 3
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if component == accountsComponent {
        return account.count
    } else if component == tasksComponent {
        return tasks.count
    } else if component == locationsComponent {
        return locations.count
    }
    return 0
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if component == accountsComponent {
        return account[row].description
    } else if component == tasksComponent {
        return tasks[row].description
    } else if component == locationsComponent {
        return locations[row].description
    }
    return nil
}

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    if component == accountsComponent {
        let label = (view as? UILabel) ?? UILabel()
        label.font = label.font.withSize(12)
        label.textAlignment = .center

        label.text = account[row].description
        return label
    }
    if component == tasksComponent {
        let label = (view as? UILabel) ?? UILabel()
        label.font = label.font.withSize(12)
        label.textAlignment = .center
        label.text = tasks[row].description
        return label
    }
    if component == locationsComponent {
        let label = (view as? UILabel) ?? UILabel()
        label.font = label.font.withSize(12)
        label.textAlignment = .center
        label.text = locations[row].description
        return label
    } else {
        let label = (view as? UILabel) ?? UILabel()
        label.font = label.font.withSize(10)
        label.textAlignment = .center
        label.text = account[row].description
        return label
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    pickerView.reloadAllComponents()

    let selectedAccount = pickerView.selectedRow(inComponent: 0)
    let selectedTask = pickerView.selectedRow(inComponent: 1)
    let selectedLocation = pickerView.selectedRow(inComponent: 2)
    let ACCOUNTS = account[selectedAccount].description
    let TASKS = tasks[selectedTask].description
    let LOCATION = locations[selectedLocation].description

    let ACCOUNTSNUMBER = account[selectedAccount].number
    let TASKSNUMBER = tasks[selectedTask].number
    let LOCATIONNUMBER = locations[selectedLocation].number

    if pickerView.selectedRow(inComponent: 2) = 10 //this is what I am unsure of how to do {

        textViewInput = "WARNING - this should be split"
    }

    else {

        textViewInput = "\(ACCOUNTSNUMBER) \(TASKSNUMBER) \(LOCATIONNUMBER)"
    }

Solution

  • I prefer switch statements over a long series of if-else if-else statements. I would personally do something like this:

    switch pickerView.selectedRow(inComponent: 2) {
    case 10, 11, 12:
        present(popUpVc, animated: true)
    default:
        textViewInput = "\(ACCOUNTSNUMBER) \(TASKSNUMBER) \(LOCATIONNUMBER)" 
    }
    

    In this case, you would replace what I put as 10, 11, 12 as whatever rows you determine that need to have the accounts split. I'm also assuming here that PopUpVC is a UIAlertController that you are using to display the warning to the user.