未在子视图

时间:2018-06-02 22:11:05

标签: ios swift uiviewcontroller uipickerview

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

在ViewController.swift中:

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应用程序中工作正常。我找不到任何与此问题相关的解决方案。

UIPickerView not showing data

2 个答案:

答案 0 :(得分:2)

问题在于viewDidLoad类的ViewController

您没有正确创建容器视图控制器。阻止选择器工作的主要问题是,SearchViewController完成时viewDidLoad没有强烈的引用,因此SearchViewController中的选择器变为nil

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)
}

答案 1 :(得分:0)

这是因为你没有加载选择器

你应该引用SearchViewController而不是引用它的视图

<强>的ViewController:

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
        }
    }
}

将loadPickerView func添加到SearchViewController

 func loadPickerView() {
        cgpicker.delegate = self
        cgpicker.dataSource = self
        self.textFieldUserName.inputView = cgpicker
        textFieldUserName.text = categories[0]
    }