我尝试实施选择器视图并将数据传递给选择器视图,但是在单击文本字段时遇到崩溃任何人都可以帮我解决此问题吗?
这是我点击文本字段后的崩溃报告
由于未捕获的异常而终止应用 'UIViewControllerHierarchyInconsistency',原因:'子视图 控制器:应该 有父视图控制器:但请求的父级是:'
var countryArray = [Country]()
var shippingAddressEditData : CustomerShippingAddress?
在视图中加载了
countryPickerView.delegate = self
countryPickerView.dataSource = self
countryTextField.inputView = countryPickerView
countryListDownloadJsonwithURL(countryAPI: countryURL)
获得国家和州的Json功能
func countryListDownloadJsonwithURL(countryAPI: String) {
let url = NSURL(string: countryAPI)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [[String:Any]] {
for item in jsonObj! {
let dict = Country(dict: item)
self.countryArray.append(dict)
}
OperationQueue.main.addOperation({
self.countryPickerView.reloadAllComponents()
})
}
}).resume()
}
这是我的模型类数据
struct Country {
let country : String
let selected : Bool
let countryId : String
var state = [State]()
init(dict:[String:Any]) {
self.country = (dict["country"] as? String)!
self.selected = (dict["selected"] as? Bool)!
self.countryId = (dict["country_id"] as? String)!
if let customAttribute = dict["state"] as? [[String: AnyObject]] {
var result = [State]()
for obj in customAttribute {
result.append(State(dict: obj))
}
self.state = result
} else {
self.state = [State]()
}
}
}
struct State {
let state : String
let stateValue : Int
init(dict: [String:Any]) {
self.state = (dict["state"] as? String)!
self.stateValue = (dict["state_value"] as? Int)!
}
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int{
if pickerView == countryPickerView{
return 1
}else {
return 1
}
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if pickerView == countryPickerView {
return countryArray.count
}
else {
return countryArray[component].state.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == countryPickerView {
self.view.endEditing(true)
return countryArray[row].country
}
else {
self.view.endEditing(true)
return countryArray[component].state[row].state
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == countryPickerView {
countryTextField.text = countryArray[row].country
countryPickerView.isHidden = true
}
else {
self.stateButton.setTitle(countryArray[component].state[row].state, for: .normal)
}
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
countryPickerView.isHidden = false
return false
}