Swift - 按下 BarButtonItem 时以编程方式创建和显示 UIPickerView

Swift - Programmatically create and display UIPickerView when BarButtonItem is pressed

最初我想在现有的 UISearchBar 旁边的 Main.storyboard 上添加一个隐藏的 UIPickerView,当单击 BarButtonItem 时,应该显示 UIPickerView;但看来我不能在给定的 space.

中同时拥有它们

因此,我最好的选择是以编程方式创建它。我已经按照现有的教程 (http://sourcefreeze.com/ios-uipickerview-example-using-swift/) and similar questions (Programmatically Create and Show UIPickerView) 进行操作,似乎我确实 (?) 有一个 UIPickerView,因为正在打印它的描述,我得到以下信息:

<UIPickerView: 0x7f86425b1fb0; frame = (100 100; 100 162); layer = <CALayer: 0x7f8642543a20>>

这是我当前代码的一部分,可能会有帮助:

AnimalTableViewController.swift

import UIKit

class AnimalTableViewController: UITableViewController, UINavigationControllerDelegate, UISearchBarDelegate, UISearchDisplayDelegate, UISearchResultsUpdating, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet var segmentedSortOption: UISegmentedControl!
    var array : NSArray = Animal.animalStruct.jsonResult["animal"] as NSArray
    var filteredArray = [[String:AnyObject]]()
    var timer = NSTimer()
    var counter:Int = 1
    var typePickerView: UIPickerView = UIPickerView()

    @IBOutlet var typeBarButton: UIBarButtonItem!
    var resultSearchController = UISearchController()

    var indexArray:String!

    @IBAction func refresh(sender: AnyObject) {

        self.tableView.reloadData()
        println("refreshed")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.typePickerView.hidden = true
        self.typePickerView.dataSource = self
        self.typePickerView.delegate = self
        self.typePickerView.frame = CGRectMake(100, 100, 100, 162)
        self.typePickerView.backgroundColor = UIColor.blackColor()
        self.typePickerView.layer.borderColor = UIColor.whiteColor().CGColor
        self.typePickerView.layer.borderWidth = 1        
        timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

        self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()

            self.tableView.tableHeaderView = controller.searchBar

            return controller
        })()
    }

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

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

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return array[row]["type1"] as String
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        typeBarButton.title = array[row]["type1"] as? String
        typePickerView.hidden = false
    }

    func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return 36.0
    }

    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 36.0
    }

    @IBAction func typePickerViewSelected(sender: AnyObject) {
        typePickerView.hidden = false
        println(typePickerView.description)
    }

}

你能帮我在按下 BarButtonItem 时显示以编程方式创建的 UIPickerView 吗?如果您还有其他问题,请尽管提问。

非常感谢。

您永远不会将 pickerView 添加为 ViewController 的子视图,您可以在 viewDidLoad() 中添加它,因为您隐藏了它。然后,当您取消隐藏它时,您的视图应该在那里。

编辑:添加代码

override func viewDidLoad() {
    super.viewDidLoad()
    self.typePickerView.hidden = true
    //other pickerView code like dataSource and delegate
    self.view.addSubview(pickerView) //will add the subview to the view hierarchy
}

使用上面的代码,现在当您按下按钮取消隐藏时,视图将显示出来。