How to add interval value into UIDatePicker in swift - ios

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.

Related

How to connect pickerviews and labels in iOS?

I have two pickerView and two Label on my ViewController.
The question is how I could connect labels to pickerView's data array (string type) and show in labels chosen pickerView cell?
#IBOutlet weak var valuetype1: UILabel!
var pickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.picker1.delegate = self
self.picker1.dataSource = self
self.picker2.delegate = self
self.picker2.dataSource = self
pickerData = ["Годы", "Месяцы", "Недели", "Дни", "Часы", "Минуты"]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
valuetype1.text = pickerData[row]
valuetype2.text = pickerData[row]
}
How could you see I connected labels (valuetype(1..2)) to pickerData but it always show in both labels one String. I need to show in first label String chosen in pickerView1 and another string in second label, chosen in pickerView2.
UIPickerViewDelegate has a method called pickerView(_:didSelectRow:inComponent:). Add this method to your VC class. This method gets called whenever the user selects a row.
Your implementation should look somewhat similar to this.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
valuetype1.text = pickerData[row]
}
For more information refer to the apple docs: https://developer.apple.com/documentation/uikit/uipickerviewdelegate/1614371-pickerview
EDIT:
When you are using the same controller as a delegate for multiple UIPickerViews, then you can check which picker view triggered the method call simply by comparing the pickerView argument on the pickerView(_:didSelectRow:inComponent:) method.
Try this:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == picker1 {
valuetype1.text = pickerData[row]
} else if pickerView == picker2 {
valuetype2.text = pickerData[row]
}
}

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

UIPickerView default row

If I want to have the default row when the view controller starts as [2] or any other row number, how do I carry that out?
var pickerData = ["2x2","4x4","6x6","8x8","12x12"]
override func viewDidLoad() {
super.viewDidLoad()
totalTilesLabel.text = pickerData[8]
display.text = sentData
self.picker.delegate = self
self.picker.dataSource = self
}
This is the UIPicker datasource and delegate.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
Set the picker view's selected row to whatever initial row you want in the viewDidLoad method. Here's Swift 3 code. Adjust as needed for Swift 2.
self.picker.selectRow(8, inComponent: 0, animated: false)

Save UIPickerView selection to NSUserDefaults swift

I am trying to save the user's selection from UIPickerView to NSUserDefaults. I have found answers in obj-c but not swift. Here is my UIPickerView code:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
race = pickerData[row]
}
And when you press a save button, it is supposed to save what is selected to NSUserDefaults. Here is my saving code:
#IBAction func save(sender: AnyObject) {
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(race, forKey: "race")
}
When I try to display the text on a label, it shows up blank:
override func viewDidLoad() {
super.viewDidLoad()
self.raceLabel.text = defaults.objectForKey("race") as? String
Let me know if my question needs any clarification, thanks.
Update your code as specified below and it will work
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
race = row
}
Every time you select a row, this function is called and the argument of the parameter row represents the row that was selected in your UIPickerView.
If you assign pickerData[row] value to race, you are assigning a value from your pickerData array at the index number represented by the row selected in your UIPickerView Object.

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