gpt4 book ai didi

ios - UIPickerView 在 PickerWheel 上返回 "?"但在 UITextField XCode 7 中返回正确的值

转载 作者:行者123 更新时间:2023-11-30 13:39:44 26 4
gpt4 key购买 nike

我对编程/编码真的很陌生(也就是说,直到这个周末我才这样做过)并且我陷入了困境。我一直在关注 youtube 上的大量教程和其他东西,我确信我已经做对了一切,但是我的选择轮显示“?”而不是正确的值,除非其中一个“?”选择后,正确的文本将显示在文本字段中。谁能发现我的错误吗?

我也查看过这个论坛的负载,但据我所知,我所做的一切都是正确的。

P.s.我现在无法嵌入图片,因为我太新了

TextField returning correct data

Corresponding data selected on PickerView displayed as a "?"

import UIKit

class ProfileCreator: UIViewController, UIPickerViewDelegate, UITextFieldDelegate, UIPickerViewDataSource {

// -------------------------------------------------------------------------------------- //

// DOB Selector From Text Field Start 1 //

@IBOutlet weak var DOBSelectorTextField: UITextField!

func textFieldDidBeginEditing(textField: UITextField) {
let datepicker = UIDatePicker()
textField.inputView = datepicker
datepicker.datePickerMode = UIDatePickerMode.Date
datepicker.addTarget(self, action: "datepickerchanged:", forControlEvents: .ValueChanged)
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}

func datepickerchanged(sender: UIDatePicker) {
let formatter = NSDateFormatter()
formatter.dateFormat = "dd MMMM yyyy"
DOBSelectorTextField.text = formatter.stringFromDate(sender.date)

}

// DOB Selector From Text Field End 1 //

// -------------------------------------------------------------------------------------- //

// Gender Selector From Text Field Start 1 //

@IBOutlet weak var GenderSelectorTextField: UITextField!
var GenderSelectorData = ["Male", "Female"]
var GenderSelectorPicker = UIPickerView()

// Gender Selector From Text Field End 1 //

// -------------------------------------------------------------------------------------- //

// Height Whole Number Text Field Start 1 //

@IBOutlet weak var HeightWholeNumberTextField: UITextField!

// Height Whole Number Text Field Start 1 //

// -------------------------------------------------------------------------------------- //

// Height Whole Number Unit Selector Text Field Start 1 //

@IBOutlet weak var HeightWholeNumberUnitSelectorTextField: UITextField!
var HeightWholeNumberUnitSelectorData = ["m", "ft"]
var HeightWholeNumberUnitSelectorPicker = UIPickerView()

// Height Whole Number Unit Selector Text Field Start 1 //

// -------------------------------------------------------------------------------------- //

// Height Decimal Number Text Field Start 1 //

@IBOutlet weak var HeightDecimalTextField: UITextField!

// Height Decimal Number Text Field End 1 //

// -------------------------------------------------------------------------------------- //

// Height Decimal Number Unit Selector Text Field Start 1 //

@IBOutlet weak var HeightDecimalNumberUnitSelectorTextField: UITextField!
var HeightDecimalNumberUnitSelectorData = ["cm", "inch"]
var HeightDecimalNumberUnitSelectorPicker = UIPickerView()

// Height Decimal Number Unit Selector Text Field End 1 //

// -------------------------------------------------------------------------------------- //

// Current Weight Whole Number Text Field Start 1 //

@IBOutlet weak var CurrentWeightWholeNumberTextField: UITextField!

// Current Weight Whole Number Text Field Start 1 //

// -------------------------------------------------------------------------------------- //

// Current Weight Whole Number Unit Selector Text Field Start 1 //

@IBOutlet weak var CurrentWeightWholeNumberUnitSelectorTextField: UITextField!
var CurrentWeightWholeNumberUnitSelectorData = ["kg", "lb"]
var CurrentWeightWholeNumberUnitSelectorPicker = UIPickerView()

// Current Weight Whole Number Unit Selector Text Field Start 1 //

// -------------------------------------------------------------------------------------- //

// Current Weight Decimal Number Text Field Start 1 //

@IBOutlet weak var CurrentWeightDecimalNumberTextField: UITextField!

// Current Weight Decimal Number Text Field End 1 //

// -------------------------------------------------------------------------------------- //

// Current Weight Decimal Number Unit Selector Text Field Start 1 //

@IBOutlet weak var CurrentWeightDecimalNumberUnitSelectorTextField: UITextField!
var CurrentWeightDecimalNumberUnitSelectorData = ["g", "oz"]
var CurrentWeightDecimalNumberUnitSelectorPicker = UIPickerView()

// Current Weight Decimal Number Unit Selector Text Field End 1 //

// -------------------------------------------------------------------------------------- //

// Close keyboard start //

func closekeyboard() {
self.view.endEditing(true)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
closekeyboard()
}

// Close keyboard end //

// -------------------------------------------------------------------------------------- //

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

// -------------------------------------------------------------------------------------- //

// DOB Selector From Text Field Start 2 //

DOBSelectorTextField.delegate = self

// DOB Selector From Text Field End 2 //

// -------------------------------------------------------------------------------------- //

// Gender Selector From Text Field Start 2 //

GenderSelectorPicker.delegate = self
GenderSelectorPicker.dataSource = self
GenderSelectorTextField.inputView = GenderSelectorPicker

// Gender Selector From Text Field End 2 //

// -------------------------------------------------------------------------------------- //

// Height Whole Number Unit Selector Text Field Start 2 //

HeightWholeNumberUnitSelectorPicker.delegate = self
HeightWholeNumberUnitSelectorPicker.dataSource = self
HeightWholeNumberUnitSelectorTextField.inputView = HeightWholeNumberUnitSelectorPicker

// Height Whole Number Unit Selector Text Field End 2 //

// -------------------------------------------------------------------------------------- //

// Height Decimal Unit Selector Text Field Start 1 //

HeightDecimalNumberUnitSelectorPicker.delegate = self
HeightDecimalNumberUnitSelectorPicker.dataSource = self
HeightDecimalNumberUnitSelectorTextField.inputView = HeightDecimalNumberUnitSelectorPicker

// Height Decimal Unit Selector Text Field End 1 //

// -------------------------------------------------------------------------------------- //

// Current Weight Whole Number Unit Selector Text Field Start 2 //

CurrentWeightWholeNumberUnitSelectorPicker.delegate = self
CurrentWeightWholeNumberUnitSelectorPicker.dataSource = self
CurrentWeightWholeNumberUnitSelectorTextField.inputView = CurrentWeightWholeNumberUnitSelectorPicker

// Current Weight Whole Number Unit Selector Text Field End 2 //

// -------------------------------------------------------------------------------------- //

// Current Weight Decimal Unit Selector Text Field Start 1 //

CurrentWeightDecimalNumberUnitSelectorPicker.delegate = self
CurrentWeightDecimalNumberUnitSelectorPicker.dataSource = self
CurrentWeightDecimalNumberUnitSelectorTextField.inputView = CurrentWeightDecimalNumberUnitSelectorPicker

// Current Weight Decimal Unit Selector Text Field End 1 //

// -------------------------------------------------------------------------------------- //

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// -------------------------------------------------------------------------------------- //

// Selectors Start 1 //

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1

}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == GenderSelectorPicker {
return GenderSelectorData.count

} else if pickerView == HeightWholeNumberUnitSelectorPicker {
return HeightWholeNumberUnitSelectorData.count

} else if pickerView == HeightDecimalNumberUnitSelectorPicker {
return HeightDecimalNumberUnitSelectorData.count

} else if pickerView == CurrentWeightWholeNumberUnitSelectorPicker {
return CurrentWeightWholeNumberUnitSelectorData.count

} else if pickerView == CurrentWeightDecimalNumberUnitSelectorPicker {
return CurrentWeightDecimalNumberUnitSelectorData.count

}

return 1

}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent comonent: Int) {
if pickerView == GenderSelectorPicker {
GenderSelectorTextField.text = GenderSelectorData[row]

} else if pickerView == HeightWholeNumberUnitSelectorPicker {
HeightWholeNumberUnitSelectorTextField.text = HeightWholeNumberUnitSelectorData[row]

} else if pickerView == HeightDecimalNumberUnitSelectorPicker {
HeightDecimalNumberUnitSelectorTextField.text = HeightDecimalNumberUnitSelectorData[row]

} else if pickerView == CurrentWeightWholeNumberUnitSelectorPicker {
CurrentWeightWholeNumberUnitSelectorTextField.text = CurrentWeightWholeNumberUnitSelectorData[row]

} else if pickerView == CurrentWeightDecimalNumberUnitSelectorPicker {
CurrentWeightDecimalNumberUnitSelectorTextField.text = CurrentWeightDecimalNumberUnitSelectorData[row]

}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == GenderSelectorPicker {
return GenderSelectorData[row]

} else if pickerView == HeightWholeNumberUnitSelectorPicker {
return HeightWholeNumberUnitSelectorData[row]

} else if pickerView == HeightDecimalNumberUnitSelectorPicker {
return HeightDecimalNumberUnitSelectorData[row]

} else if pickerView == CurrentWeightWholeNumberUnitSelectorPicker {
return CurrentWeightWholeNumberUnitSelectorData[row]

} else if pickerView == CurrentWeightDecimalNumberUnitSelectorPicker {
return CurrentWeightDecimalNumberUnitSelectorData[row]

}

return ""

}

}

// Selectors End 1

}

最佳答案

您正在创建几个新的 UIPickerView,而不是使用 IBOutlet 连接 xib/storyboard 中已存在的选择器 View ,这些 View Controller 显示在 View Controller 中(因为您没有添加新创建的选择器 View )选择器作为 subview )。

确保使用 IBOutlets 并设置界面构建器中已存在的委托(delegate)/数据源。这有望解决您的问题。

关于ios - UIPickerView 在 PickerWheel 上返回 "?"但在 UITextField XCode 7 中返回正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35711041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com