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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33027355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:56:50  来源:igfitidea点击:

Swift - Programmatically create and display UIPickerView when BarButtonItem is pressed

iosiphoneswiftuipickerviewuibarbuttonitem

提问by Javz

Initially I wanted to add a hidden UIPickerView onto the Main.storyboard alongside my existing UISearchBar and when a BarButtonItem is clicked, the UIPickerView should be displayed; but it appears I cannot have them both at once in a given space.

最初,我想在 Main.storyboard 上添加一个隐藏的 UIPickerView,与我现有的 UISearchBar 一起,当点击 BarButtonItem 时,应该显示 UIPickerView;但似乎我不能在给定的空间中同时拥有它们。

So instead, my best alternative was to create it programmatically. I've followed existing tutorials (http://sourcefreeze.com/ios-uipickerview-example-using-swift/) and similar questions (Programmatically Create and Show UIPickerView) and seems like I do (?) have a UIPickerView as the description of it is being printed and I get the following:

因此,我最好的选择是以编程方式创建它。我遵循了现有的教程(http://sourcefreeze.com/ios-uipickerview-example-using-swift/)和类似的问题(以编程方式创建和显示 UIPickerView),似乎我(?)有一个 UIPickerView 作为描述它正在打印,我得到以下信息:

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

Here is part of my current code which may be of help:

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

AnimalTableViewController.swift

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

}

Please could you help me display the programmatically created UIPickerView when the BarButtonItem is pressed? If you have any more questions, please do ask.

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

Many thanks.

非常感谢。

采纳答案by pbush25

You never add the pickerViewas a subview of the ViewController, which you can do in viewDidLoad()since you're hiding it. Then when you unhide it your view should be there.

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

EDIT: Added Code

编辑:添加代码

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
}

With the above code, now when you unhide it on button press the view will show up.

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