未在子视图中调用的UIPickerView委托怎么解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (94)

我有一个简单的iOS应用程序,它使用UISegmentedControl在两个选项卡之间切换。

在ViewController中:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var segment: UISegmentedControl!
    @IBOutlet weak var viewContainer: UIView!
    var searchView: UIView!
    var favView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    searchView = SearchViewController().view
    favView = FavoriteViewController().view
    viewContainer.addSubview(favView)
    viewContainer.addSubview(searchView)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


@IBAction func segmentChange(_ sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex{
    case 0:
        viewContainer.bringSubview(toFront: searchView)
        break
    case 1:
        viewContainer.bringSubview(toFront: favView)
        break
    default:
        break
    }
}

}

在第一个选项卡中,我有一个UIPickerView作为Textfield的输入。

import UIKit

class SearchViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var keywordField: UITextField!
    @IBOutlet weak var categoryField: UITextField!
    @IBOutlet weak var distanceField: UITextField!
    @IBOutlet weak var locationField: UITextField!

    var cgpicker = UIPickerView()
    let categories = ["Default", "Airport", "Amusement Park", "Aquarium", "Art Gallery", "Bakery", "Bar"]


override func viewDidLoad() {
    super.viewDidLoad()
    cgpicker.delegate = self
    cgpicker.dataSource = self
    categoryField.inputView = cgpicker
    categoryField.text = categories[0]
}

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    print("picker working")
    return categories.count
}

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    categoryField.text = categories[row]
    categoryField.resignFirstResponder()
}


}

但是,当我单击Textfield时,PickerView不显示数据。事实上,委托和数据源不会被调用,因为“print”从未被调用过。然而,在一个简单的UIView应用程序中,相同的代码工作得很好。我找不到任何与这个问题有关的解决办法。

提问于
用户回答回答于

这是因为你没有装载拾取器

视图控制器:

class ViewController: UIViewController {

    @IBOutlet weak var segment: UISegmentedControl!
    @IBOutlet weak var viewContainer: UIView!
    var searchController: SearchViewController!
    var favController: FavoriteViewController!

    override func viewDidLoad() {
        super.viewDidLoad()
        searchController = SearchViewController()
        favController = FavoriteViewController()
        viewContainer.addSubview(searchController.view)
        viewContainer.addSubview(favController.view)
        searchController.loadPickerView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    @IBAction func segmentChange(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex{
        case 0:
            viewContainer.bringSubview(toFront: searchController.view)
             searchController.loadPickerView()
            break
        case 1:
            viewContainer.bringSubview(toFront: favController.view)
            break
        default:
            break
        }
    }
}

添加loadPickerViewfuncSearchViewController

 func loadPickerView() {
        cgpicker.delegate = self
        cgpicker.dataSource = self
        self.textFieldUserName.inputView = cgpicker
        textFieldUserName.text = categories[0]
    }
用户回答回答于

问题在于viewDidLoad你的ViewController

更新为ViewController viewDidLoad这样的东西:

override func viewDidLoad() {
    super.viewDidLoad()

    let searchVC = SearchViewController()
    searchView = searchVC.view
    addChildViewController(searchVC)
    // Should set searchView frame or constraints
    viewContainer.addSubview(searchView)
    searchVC.didMove(toParentViewController: self)

    let favVC = FavoriteViewController()
    favView = favVC.view
    addChildViewController(favVC)
    // Should set favView frame or constraints
    viewContainer.addSubview(favView)
    favVC.didMove(toParentViewController: self)
}

扫码关注云+社区

领取腾讯云代金券