Inizialmente volevo aggiungere un UIPickerView nascosto a Main.storyboard accanto al mio UISearchBar esistente e quando si fa clic su BarButtonItem, deve essere visualizzato UIPickerView; ma sembra che non posso averli entrambi contemporaneamente in un dato spazio.Swift - Crea e visualizza a livello di codice UIPickerView quando viene premuto BarButtonItem
Quindi, invece, la mia alternativa migliore era crearlo a livello di programmazione. Ho seguito lezioni private esistenti (http://sourcefreeze.com/ios-uipickerview-example-using-swift/) e su questioni analoghe (Programmatically Create and Show UIPickerView) e sembra che ho un UIPickerView come la descrizione di esso è in fase di stampa e ottengo il seguente (?):
<UIPickerView: 0x7f86425b1fb0; frame = (100 100; 100 162); layer = <CALayer: 0x7f8642543a20>>
Qui fa parte del mio codice corrente che può essere di aiuto:
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)
}
}
prega potreste aiutarmi visualizzo l'UIPI di programmazione creato ckerView quando viene premuto BarButtonItem? Se avete altre domande, per favore chiedete.
Molte grazie.
Potrebbe per favore mostrarmi un esempio? Poiché non sono del tutto sicuro di cosa intendi con questo. Vorresti che lo aggiungessi al ViewController? Sebbene sia dichiarato sul TableViewController. Grazie – Javz
@ Javz Ho aggiornato la mia risposta con il codice per mostrarti. – pbush25
NON POSSO credere che mi sia sfuggito qualcosa di così semplice ..... Grazie mille !! – Javz