将数据从 uipickerview 传递到 uilabel 的问题

Issues passing data from uipickerview to uilabel

我在按“完成”时从选择器中获取所选结果以显示在我的描述中时遇到问题。当 运行 应用程序的描述将打印 ["Male", "Female"]。最终结果应该只将所选选项插入到描述中。

我是 swift 的新手,不确定我做错了什么才能得到这个结果。对此有任何帮助,我们将不胜感激。代码如下:

class OnboardingViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var NextButton: UIButton!
@IBOutlet weak var Description: UILabel!

let gender = ["Male", "Female"]

var genderPicker = UIPickerView()
var toolBar = UIToolbar()

var slides: [OnboardingSlide] = []

override func viewDidLoad() {
    super.viewDidLoad()

    NextButton.layer.cornerRadius = NextButton.frame.width / 2
    NextButton.layer.masksToBounds = true
    NextButton.alpha = 0
    
    Description.text = "An ideal daily intake of calories varies depending on age, metabolism and levels of physical activity, among other things. Generally, the recommended daily calorie intake is 2,000 calories a day for women and 2,500 for men."

    Description.numberOfLines = 0
    Description.lineBreakMode = .byTruncatingTail
    view.addSubview(Description)
    
}

@IBAction func genderSelectButton(_ sender: UIButton) {

        genderPicker = UIPickerView.init()
        genderPicker.delegate = self
        genderPicker.dataSource = self
        genderPicker.backgroundColor = UIColor.white
        genderPicker.setValue(UIColor.black, forKey: "textColor")
        genderPicker.autoresizingMask = .flexibleWidth
        genderPicker.contentMode = .center
        genderPicker.frame = CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 300)
        self.view.addSubview(genderPicker)
                
        toolBar = UIToolbar.init(frame: CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 50))
        toolBar.items = [UIBarButtonItem.init(title: "Done", style: .done, target: self, action: #selector(onDoneButtonTapped))]
        self.view.addSubview(toolBar)
    
    Description.alpha = 0
    
}

@objc func onDoneButtonTapped() {
    toolBar.removeFromSuperview()
    genderPicker.removeFromSuperview()
    Description.text = "You are \(gender)."
    Description.alpha = 100
    NextButton.alpha = 100
    
}

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return gender.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return gender[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    Description.text = gender[row]
    print(gender[row])
}

@IBAction func nextButtonClicked(_ sender: Any) {
}

您可以在用户更改选择时收听 pickerView(_:didSelectRow:inComponent) 并记录行值,或者在用户点击时向选择器询问其选定的行(使用 selectedRow(inComponent:) 方法)你的完成按钮。

您的 Description.text = "You are \(gender)." 代码没有意义,因为 gender 似乎是一组性别值。我希望读取 Description.text = "You are \(gender[selectedRow])."(假设您将所选行保存到实例变量 selectedRow。)

注意实例变量应该以小写字母开头,类型应该以大写字母开头。因此 Description 应该是 description.

在 Swift 中有一个严格的约定,即一致地使用大小写(Class 并且类型名称应该是大写的“驼峰式”,变量和值应该用小驼峰式命名。

另一个想法:

我建议将 Gender 设为枚举:

enum Gender: String, CaseIterable {
   case male
   case female
}

然后您可以使用 Gender.allCases[index] 使用基于 0 的索引获取性别,并使用

获取性别的字符串值
let aGender: Gender = .male
let genderName: String = aGender.rawValue

您还可以使用 Gender(rawValue:) 从字符串创建性别。考虑这段代码:

func genderFromString(_ aString: String) {
    if let genderFromString = Gender(rawValue: aString) {
        print("genderFromString for '\(aString)' = \(genderFromString)")
    } else {
        print("Can't convert '\(aString)' to a Gender")
    }
}

genderFromString("male")

genderFromString("Wombat")

输出:

genderFromString for 'male' = male
Can't convert 'Wombat' to a Gender