Delegate Methods in Swift for UIPickerView - ios

Just getting started in Swift and having trouble calling the delegate methods for a UIPickerView
So far I have added the UIPickerViewDelegate to my class like so:
class ExampleClass: UIViewController, UIPickerViewDelegate
I have also created my UIPickerView and set the delegate for it:
#IBOutlet var year: UIPickerView
year.delegate = self
Now I am just having trouble turning the following into Swift code:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
Any help would be appreciated

That's actually a method in the UIPickerViewDataSource protocol, so you'll want to make sure you set your picker view's dataSource property as well: year.dataSource = self. The Swift-native way seems to be to implement protocols in class extensions, like this:
class ExampleClass: UIViewController {
// properties and methods, etc.
}
extension ExampleClass: UIPickerViewDataSource {
// two required methods
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
return 5
}
}
extension ExampleClass: UIPickerViewDelegate {
// several optional methods:
// func pickerView(pickerView: UIPickerView!, widthForComponent component: Int) -> CGFloat
// func pickerView(pickerView: UIPickerView!, rowHeightForComponent component: Int) -> CGFloat
// func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String!
// func pickerView(pickerView: UIPickerView!, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString!
// func pickerView(pickerView: UIPickerView!, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView!
// func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
}

The delegate isn't responsible for calling that method. Instead it is called by the UIPickerView's data source. These are the two functions called by the UIPickerView data source that you need to implement:
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int
To ensure these functions get called, your class should also implement the data source protocol:
class ExampleClass: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
And your picker's data source should be set:
year.dataSource = self

Related

PickerView shows only '?'

This is my source code and what I have done is make picker view on storyBoard. Make IBOutlet in this controller by contorl+drag.
It can be compiled however, only '?' appears in the picker view.
Where is the problem?
import UIKit
class SelectViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{
var songNames = ["test1","test2","test3"]
#IBOutlet weak var songPicker: UIPickerView!
override func viewDidLoad(){
songPicker.delegate = self
songPicker.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return songNames.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int) -> String? {
return songNames[row]
}
}
You have missed the forComponent parameter from the dataSource method. Add it in your titleForRow function like this:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return songNames[row]
}
This should fix the problem you are having.

extract value from UIpickerview as variable

so i have this code for a UIpickerview and i want to get the value selected as a variable to use later, i tried using a variable gravity but it says "initialisation of variable gravity was never used...." here's my code:
var g = ["9.807", "3.711"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return g.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return g[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var gravity = Float(g[row])
}
You need to declare the var outside of the scope of your method:
var gravity: Float?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
gravity = Float(g[row])
}
A local variable scope is limited to the method and as you don't use it within the method you get a warning for that.
This is Swift not Javascript :)
Your gravity var is local, so the given warning makes total sense (i.e., its scope is limited to the given function).
You should use the following UIPickerView method to get the currently selected index:
func selectedRow(inComponent component: Int) -> Int
For instance:
let gIndex = pickerView.selectedRow(inComponent: 0)
let gValue = Float(g[gIndex])!
Note that you do not even need to implement the didSelectRow delegate method to get this information.
As correctly pointed out in the comments, you of course need to declare and bind your UIPickerView to an instance var of your view controller:
#IBOutlet weak var pickerView: UIPickerView!
To use pickerView, first mention its DataSource and Delegate.
UIPickerViewDataSource, UIPickerViewDelegate
Now create this by right clicking on pickerView -> to the ViewContoller.swfit.You will get like:
#IBOutlet var pickerView: [UIPickerView]!
Create an array which contains the content you want to show in pickerView. Just like this:
var array:[String] = ["David","Mikel","Akhil","Akshay","Zlatan"]
Now you have to use pickerView functions. These functions can only be used if you mention DataSource and Delegate in class. The functions are:
// number of rows or number of components
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// number of rows that we have to set
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return array.count
}
// set title in picker view according to the elements
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array[row]
}
// when an element is selected, store in a string
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerString = array[row]
print(pickerString) // pickerString is String type. You can create this, to show the content selected.
}

how to create a list appears from the bottom in Swift

how can I do a list like this ?
can you give me the name of the method ?
is it PickerView ?
I want a filed ruled City Name and when clicked in ..
a list from the bottom appears
see the example here
For this you should use UIPickerView. And set datasource and delegate methods. This is small example.
Some key points of the implementation.
extension ViewController : UIPickerViewDataSource {
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return "\(row)"
}
}
extension ViewController : UIPickerViewDelegate {
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 10
}
}

How to add interval value into UIDatePicker in swift

I want to know how to add interval value into UIDateTimePicker in swift
8am-8:30am
8:30am-9am
9am-9:30am
9:30am-10am
10am-10:30am
10:30am-11am
You can try something like this. Add one UIPickerView in your xib or your storyboard viewController after that set the delegate and datasoucre of that UIPickerView with your viewController. Now create one array of string like this.
var timeArr: [String] = [String]()
Now initialize this array in viewDidLoad like this
override func viewDidLoad() {
super.viewDidLoad()
self.timeArr = ["8:00AM - 8:30AM", "8:30AM - 9:00AM", "9:00AM - 9:30AM", "9:30AM - 10:00AM"]
}
Now add the delegate and datasource method of UIPicker in your ViewController like this
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.timeArr.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.timeArr[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(self.timeArr[row])
}
This will create Picker that you want to select time.

UIPickerView didSelectRow not called

(Swift, Xcode6, iOS8, iPhone)
In my UIPickerView control, didSelectRow is not being called.
Yes, I have set Splash as the delegate (and datasource) for the picker.
Splash extends the UIViewController class
All the other functions work. No errors, the picker spins, has five rows, and displays the row number as defined in the other functions.
When does the didSelectRow method get called? Do I need to implement a Done button of some kind, or will this method fire when the user stops spinning? Do I need to implement a separate notification? tyvm :)
class Splash: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet var countryPicker : UIPickerView = nil
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
return 5
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String {
return "\(row)"
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, forComponent component: Int) -> Int {
println("Row: \(row)")
return row
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You have the wrong signature, pickerView:didSelectRow:forComponent: returns void, not an integer. Try:
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, forComponent component: Int) {
println("Row: \(row)")
}
Swift 4.2
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("selected row = \(row)") }

Resources