multiple rows selection for pickerview in swift 3.0 - swift3

I want to select a multiple rows in pickerview in swift
this is for single row selection
`import UIKit
class ViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource{
#IBOutlet weak var titlelabel: UILabel!
#IBOutlet weak var pickerview: UIPickerView!
var cars = ["benz","audi","lombagini","rangerrover","bently"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerview.delegate = self
pickerview.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return cars.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return cars[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
titlelabel.text = cars[row]
}
}`
did any one have any idea to select multiple rows in picker view
in objective-c AlPickerview but it is not working.

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0{
return bloodgroup.count
}
else if component == 1 {
return districk.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0{
return bloodgroup[row]
}
else if component == 1 {
return districk[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0{
print(bloodgroup[row])
}
else if component == 1 {
print(districk[row])
}
}

Related

Possible to create a function that returns PickerView with data?

I'm trying to create a framework that does an API call and creates a PickerView with the populated data from the API call. The iOS client will import this framework and call that one exposed function that should return the PickerView with the loaded data in it.
I managed to create a function that creates the PickerView but can't figure out how to insert the data inside the PickerView.
// Framework Side
public static func returnPickerView() -> UIPickerView {
let apple = ["apple", "orange", "durian", "banana"]
let customPicker = UIPickerView()
customPicker.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
customPicker.layer.borderColor = UIColor.black.cgColor
customPicker.layer.borderWidth = 1
return customPicker
}
Solution to this problem was to create a custom class like what JuicyFruit said.
class CustomPickerView: UIPickerView {
override init(frame: CGRect) {
super.init(frame: frame)
dataSource = self
delegate = self
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
extension CustomPickerView: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
...
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
...
}
}
extension CustomPickerView: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
...
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
...
}
}

change global textcolor of UIPickerView possible?

I am having trouble with two of my pickerViews. I am wondering if it is possible to change the global text color of my pickerViews in the AppDelegate file?
I know some stuff can be done like:
UIPickerView.appearance().backgroundColor = color6
UIPickerView.appearance().tintColor = color4
If I need to write it in my code manually in each pickerView. What sort of pickerView should it be coded into, titleForRow?
Try to use the following class. I have subclassed the UIPickerView and specified colours inside. This is working fine. when you set the items. it will set delegate and datasource to self and will do the job for you.
import UIKit
class CustomUIPickerView: UIPickerView{
let textColor: UIColor = .white
let backGroundColor: UIColor = .blue
let _tintColor: UIColor = .white
var items: [String]?{
didSet{
self.delegate = self
self.dataSource = self
self.reloadAllComponents()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit(){
self.backgroundColor = backGroundColor
self.tintColor = _tintColor
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
}
extension CustomUIPickerView: UIPickerViewDataSource{
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.items?.count ?? 0
}
}
extension CustomUIPickerView: UIPickerViewDelegate{
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let string = self.items?[row] ?? ""
return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: textColor])
}
}
However if you want to handle the delegate and datasource in your view controller then you can take the advantage of using Extensions here is how:
extension String{
func stringForPickerView() -> NSAttributedString{
let color: UIColor = .white
return NSAttributedString(string: self, attributes: [NSAttributedStringKey.foregroundColor: color])
}
}
then in your ViewController:
extension ViewController: UIPickerViewDelegate{
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return self.items[row].stringForPickerView() // from Extension
}
}

Button title is cropped. Why?

I have button and Picker View. If i'm trying to choose item in Picker View and use text for Button's tittle it have cropped text. Why?
This is my code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
{
let array = ["Incredible", "Fantastic"]
#IBOutlet weak var pickerView: UIPickerView!
#IBOutlet weak var button: UIButton!
override func viewDidLoad()
{
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return array.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
button.setTitle(array[row], for: .normal)
return array[row]
}
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
{
return 50
}
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
}
Screenshots of Device emulator & XCODE's Storyboard:

How to call method in UIPickerView subclass from another view controller?

I have a UIPickerView that I am subclassing, which works great. However, I have a method that I need to call to "reset" the UIPicker from the UIViewController in which it's being viewed. However, it never fires. How do I call func resetPicker() from another view controller?
import Foundation
import UIKit
class ScoreClockPicker: UIPickerView {
//PickerView
let timerMinutesArray = Array(00...20)
let timerSecondsArray = Array(00...59)
let periodArray = ["1st", "2nd", "3rd", "OT", "SO"]
//Picker return values
var numberOfRowsInComponentReturnValue = 0
var titleForRowReturnValue = ""
var widthForComponentReturnValue = ""
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = self
self.dataSource = self
}
func resetPicker() {
print("resetPicker")
self.selectRow(0, inComponent: 0, animated: true)
self.selectRow(0, inComponent: 1, animated: true)
self.selectRow(0, inComponent: 3, animated: true)
}
}
extension ScoreClockPicker: UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
numberOfRowsInComponentReturnValue = periodArray.count
} else if component == 1 {
numberOfRowsInComponentReturnValue = timerMinutesArray.count
} else if component == 2 {
numberOfRowsInComponentReturnValue = 1
} else if component == 3 {
numberOfRowsInComponentReturnValue = timerSecondsArray.count
}
return numberOfRowsInComponentReturnValue
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 4
}
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
var componentWidth = 0
if component == 0 {
componentWidth = 140
} else if component == 1 {
componentWidth = 40
} else if component == 2 {
componentWidth = 30
} else if component == 3 {
componentWidth = 40
}
return CGFloat(componentWidth)
}
}
extension ScoreClockPicker: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("didSelectRow")
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
titleForRowReturnValue = periodArray[row]
} else if component == 1 {
titleForRowReturnValue = String(describing: timerMinutesArray[row])
} else if component == 2 {
titleForRowReturnValue = ":"
} else if component == 3 {
titleForRowReturnValue = String(format: "%02d",timerSecondsArray[row])
}
return titleForRowReturnValue
}
}
EDIT:
The following doesn't work from the viewController calling the ScoreClockPicker:
import UIKit
class ScoreClockViewController: UIViewController {
#IBOutlet weak var resetButton: UIButton!
#IBOutlet weak var okButton: UIButton!
var picker: ScoreClockPicker?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//Hide the Navigation Bar
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
//#IBOutlet
#IBAction func reset(_ sender: Any) {
print("reset")
picker?.resetPicker() //Call the ScoreClockPicker
// picker?.selectRow(0, inComponent: 0, animated: true)
// picker?.selectRow(0, inComponent: 1, animated: true)
// picker?.selectRow(0, inComponent: 3, animated: true)
}
#IBAction func ok(_ sender: Any) {
}
}
From your code you haven't initialized the picker object that you are trying to use in button action. So simply initialized the picker before using it.
If you are making outlet of ScoreClockPicker then
#IBOutlet var picker: ScoreClockPicker!
OR you can initialized picker in viewDidLoad
self.picker = ScoreClockPicker()

PickerView: Type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'

I am using code from a sample project. It does work in the sample project, but not in my project.
SettingsViewController.swift:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
let days:[String] = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return days.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return days[row]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am using Xcode 8.2.1, Swift 2.3
The method signature for numberOfComponents(in:) changed from iOS 9.3 to iOS 10. Since you are targeting a legacy version of Swift, replace your current implementation of numberOfComponents(in:) with the appropriate version below.
Swift 2.3
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
Swift 3
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
The API changes can be seen here: https://developer.apple.com/library/content/releasenotes/General/iOS10APIDiffs/Swift/UIKit.html
Modified UIPickerViewDataSource
Declaration
From
protocol UIPickerViewDataSource : NSObjectProtocol {
func numberOfComponentsInPickerView(_ pickerView: UIPickerView) -> Int
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
}
To
protocol UIPickerViewDataSource : NSObjectProtocol {
func numberOfComponents(in pickerView: UIPickerView) -> Int
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
}