分配与UIPickerView上显示的输出值不同的输出值

时间:2018-04-22 17:14:57

标签: swift xcode uipickerview

我昨天在这个网站上搜索了所有这个问题的答案,但我找不到这个。我是一名业余编码员,真的只是为一个小型组织做这件事,看看我们是否可以帮助改进流程。

这是我正在尝试做的事情。我正在尝试创建一个允许某人选择帐号,任务的应用程序,然后它会输出一个完整帐号的数字,这些号码可以放在收据上。

例如,有人选择帐号12345678910并选择“清洁浴室”。我希望该应用程序吐出一个完整的帐号,如12345678910 58392,其中58932与“清洁浴室”相关联,但实际上并未显示在PickerView上。我一直遇到麻烦。编码是我一直想要学习的东西,但是我一直都是在自己的时间里做的,所以请原谅业余爱好者如果有错误的话。我一直在使用YouTube视频。

感谢任何帮助。非常感谢! Sample app screen-shot

import UIKit

class Account {
    var account: String
    var jlCode: [String]

    init(account:String, jlCode:[String]) {
        self.jlCode = jlCode
        self.account = account
    }
}

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var pickerView: UIPickerView!
    @IBOutlet weak var accountLbl: UILabel!

    var accounts = [Account]()

    @IBAction func showPopup(_ sender: Any) {
        let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpViewController
        self.addChildViewController(popOverVC)

        popOverVC.view.frame = self.view.frame
        self.view.addSubview(popOverVC.view)

        popOverVC.didMove(toParentViewController: self)
    }

    override func viewDidLoad() {

        pickerView.delegate = self
        pickerView.dataSource = self

        //This area will have to be changed once you get the right information.

        accounts.append(Account(account: "12345678910", jlCode: ["task 1", "task 2", "task 3", "task 4"]))
        accounts.append(Account(account: "1112131415", jlCode: ["Task 1"]))
        accounts.append(Account(account: "1617181920", jlCode: ["task 1", "task 2", "task 3", "task 4"]))
        accounts.append(Account(account: "2122232425", jlCode: ["task 1", "task 2", "task 3", "task 4"]))

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return accounts.count
        } else {
            let selectedAccount = pickerView.selectedRow(inComponent: 0)
            return accounts[selectedAccount].jlCode.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return accounts[row].account
        } else {
            let selectedAccount = pickerView.selectedRow(inComponent: 0)
            return accounts[selectedAccount].jlCode[row]
        }
    }

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

        let selectedAccount = pickerView.selectedRow(inComponent: 0)
        let selectedjlCode = pickerView.selectedRow(inComponent: 1)
        let account = accounts[selectedAccount].account
        let jlCode = accounts[selectedAccount].jlCode[selectedjlCode]

        accountLbl.text = "Account Number: \(account)\nJL Code: \(jlCode)"
    }
}

1 个答案:

答案 0 :(得分:0)

如果,那是一个很大的 IF ,我理解你的问题是你没有任何东西将任务的 description 与与之关联的数值相关联。任务。换句话说,您似乎只是在努力模拟数据。

我认为以下内容可能会帮助您朝着正确的方向前进:

struct Account {
    var number: Int
    var tasks: [Task]

    var description: String { return "\(number)" }
}

struct Task {
    var number: Int // or JLNumber, or whatever you want to call it
    var description: String
}

这会让你设置像这样的帐户:

let account = Account(number: 12345678910, tasks: [Task(number: 58932, description: "Cleaning the bathroom")])

您将在pickerView中显示.description,并在有意义的地方使用.number

如果 任务的 ,那么(如果没有更改代码,它们永远不会改变)你应该看看enums而不是而不是使用字符串和整数动态创建它们......