I have two different UIPickerViews in my View. They work great when I set the dataSource and the delegate to the View they are hosted in via the storyboard, but when I try to do that via code as described below, it does not work.
Both pickers shall have different data to display (and maybe even different behaviours for the delegate). Thus I would like to connect them to different data sources programmatically.
I tried to create my own class implementing the UIPickerViewDataSource- and UIPickerViewDelegate-Protocols and connecting objects of that class to my PickerViews, but it does not work. An exception is thrown at runtime terminating with uncaught exception of type NSException stating this:
2015-01-09 17:50:05.333 Pet Stats[4953:244338] -[NSConcreteMapTable numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7b4616d0
2015-01-09 17:50:05.338 Pet Stats[4953:244338] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMapTable numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7b4616d0'
How can I get this to work? What did I miss? Here is my code:
WeightWheelController.swift
import UIKit
class WeightWheelController: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
let ElementCount: Int!
init(pickerInterval: Int) {
ElementCount = pickerInterval
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return ElementCount
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return String(row + 1)
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
println("External Controller:" + String(row + 1))
}
}
WeightWheelInputViewController.swift
import UIKit
class WeightWheelInputViewController: UIViewController {
#IBOutlet weak var picker1: UIPickerView!
#IBOutlet weak var picker2: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
//picker attached to c1 should show number from 1 to 150
let c1 = WeightWheelController(pickerInterval: 150)
//picker attached to c1 should show number from 1 to 10
let c2 = WeightWheelController(pickerInterval: 10)
picker1.dataSource = c1
picker1.delegate = c1
picker2.dataSource = c2
picker2.delegate = c2
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
BRIEF UPDATE:
In this question I have found that you can use different tags for different picker views. That would be one option; yet, I don't like it. I would like to rather follow a MVC'ish approach and connect different controllers to each picker. Isn't that possible in any way?
Both delegate and datasource are unowned references. This means that c1 and c2 will get released as soon as you go out of scope. Try declaring c1 and c2 as properties of the class.
Unowned references do not create a strong hold on the referred object (a.k.a. they don't increase the retain count in order to prevent ARC from deallocating the referred object).
Also make sure you remove the delegate and datasource properties of the pickerviews from the interface builder.
class WeightWheelInputViewController: UIViewController {
#IBOutlet weak var picker1: UIPickerView!
#IBOutlet weak var picker2: UIPickerView!
var c1 : WeightWheelController!
var c2 : WeightWheelController!
override func viewDidLoad() {
super.viewDidLoad()
c1 = WeightWheelController(pickerInterval: 150)
c2 = WeightWheelController(pickerInterval: 10)
picker1.dataSource = c1
picker1.delegate = c1
picker2.dataSource = c2
picker2.delegate = c2
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Related
When I traverse this UIPickerView from top down, the row is reported correctly for each element. However, traversing the UIPickerView from bottom to top, when "Choice 3" is tapped, the row resets itself to zero where it should actually be 2. I added the label for visual proof -- it flashes "2", then resets itself to zero when traversing the UIPickerView as described. Obviously, not being able to depend on the relationship between display element and actual row being accurate breaks things. No idea why this isn't working...
class ViewController3: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var picker: UIPickerView!
#IBOutlet weak var textLabel: UILabel!
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 4
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String? {
textLabel.text = String(row)
return pickerValues[row]
}
let pickerValues = ["Choice 1", "Choice 2", "Choice 3","Choice 4"]
If the goal is to change a label every time the user turns the "wheel" of the picker view, implement the delegate method pickerView(_:didSelectRow:inComponent:).
I have 2 UITableViews in a single View Controller which represent a 23 cells hours table view and 60 cells minutes table view. What i want to achieve is i can scroll both table view like rolling wheel like when we have scrolled to the end, it can still be scrollable to the first table view by scrolling bottom and vice versa.
I found something like this : https://github.com/bharath2020/UITableViewTricks/tree/master/CircleView/BBTableView , but too bad it's in Obj-c.
I want to be able to achieve the following :
Where the hours and minutes can be scrolled infinitely like from 12 -> 0 or 59 -> 0
How do i do something like that in swift?
Okay So just tested out for you. Here is an example for the circular UIPickerView.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let maxRows = 10
private let maxElements = 10000
#IBOutlet weak var myPickerView: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
self.myPickerView.dataSource = self;
self.myPickerView.delegate = self;
myPickerView.selectRow(maxElements / 2, inComponent: 0, animated: false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return maxElements
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let myRow = row % maxRows
let myString = String(format: "%i", myRow)
return myString
}
}
Now as far as creating a UIPickerPickerView with two components is concerned, You can find easily on google.
I hope it helps you to solve the problem.
I have a ViewController with a UIPickerView as a single control myPickerView which is of a class MyPickerView which I created as a sub-class of UIPickerView. I invoke myPickerView in ViewController viewDidLoad by myPickerView.viewDidLoad. However, this does not execute the source functions of MyPickerView.
I need a clarification of how I can make this work. My reason for MyPickerView is that it has a lot of special code that I did not want to clutter up the main ViewController. See the example code below:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myPickerView: MyPickerView!
override func viewDidLoad() {
super.viewDidLoad()
myPickerView.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
var gSep = ","
class MyPickerView: UIPickerView , UIPickerViewDataSource, UIPickerViewDelegate {
var pickerData = [[" "],[" "]]
var category = [""]
var subCategory = [""]
var dictMenuList = [String:String]()
//MARK:- category/subcategory picker
func viewDidLoad() {
println("MyPickerView: viewDidLoad")
dictMenuList = ["Medical":"Sub-Cat 1.1,Sub-Cat 1.2,Sub-Cat 1.3,Sub-Cat 1.4,Sub-Cat 1.5,Sub-Cat 1.6,Sub-Cat 1.7",
"Taxes": "Sub-Cat 2.1,Sub-Cat 2.2,Sub-Cat 2.3,Sub-Cat 2.4",
"Bills": "Sub-Cat 3.1,Sub-Cat 3.2,Sub-Cat 3.3,Sub-Cat 3.4,Sub-Cat 3.5,Sub-Cat 3.6,Sub-Cat 3.7"]
println("MyPickerView dictMenuList: \(dictMenuList)")
self.reloadAllComponents()
let firstKey = self.loadPickerWithCategory(0)
self.loadPickerWithSubCategory(firstKey)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
println("MyPickerView: numberOfComponentsInPickerView \(pickerData.count)")
return pickerData.count
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData[component].count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
let selectedKey = category[row]
loadPickerWithSubCategory(selectedKey)
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[component][row]
}
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
var pickerLabel = UILabel()
pickerLabel.textColor = UIColor.blackColor()
pickerLabel.text = pickerData[component][row]
pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 17)
pickerLabel.textAlignment = NSTextAlignment.Center
return pickerLabel
}
func loadPickerWithCategory (row: Int) -> String{
println("loadPickerWithCategory")
category = [String](dictMenuList.keys)
println("MyPickerView: category: \(category)")
if category.isEmpty {
return ""
}
let n1 = dictMenuList.count
pickerData[0].removeAll(keepCapacity: true)
for i in 0 ..< n1
{
pickerData[0].append(category[i])
}
return category[row]
}
func loadPickerWithSubCategory (key: String) {
println("MyPickerView: loadPickerWithSubCategory")
let x = dictMenuList[key]
subCategory = x!.componentsSeparatedByString(gSep)
let n1 = subCategory.count
pickerData[1].removeAll(keepCapacity: true)
if subCategory.isEmpty {
return
}
for i in 0 ..< n1
{
pickerData[1].append(subCategory[i])
}
self.reloadAllComponents()
}
}
The method viewDidLoad is a view controller method, not a view method. A UIPickerView is a subclass of UIView, not UIViewController, so the system will not call your viewDidLoad method.
You need to override one or more of the init methods.
If you're loading your picker view from a Storyboard or XIB, you probably want to override initWithCoder.
If you're creating your picker in code, you probably want to override initWithFrame.
I sometimes create a method setup that I call from both initWithCoder: and from initWithFrame:. That way my setup code gets called regardless of how the view object is loaded.
I vaguely remember reading that there is a better way of handling this dueling initializers problem in Swift, but I don't remember what it is. (I'm still learning Swift.)
EDIT:
It just occurs to me that you can use the method awakeFromNib to do setup after your view has been loaded and all of it's outlets are set up. That's roughly equivalent to the viewDidLoad call for view controllers. I should have thought of that sooner.
(awakeFromNib is a method of NSObject, so it's a bit hard to find if you don't know it exists.)
First of all viewDidLoad() is a method of the UIViewController class and is called after the controller's view is loaded into the memory. Read more here. You can not use it in views.
So you should implement an init method inside your custom picker class. I'd recommend to override initWithFrame and initWithCoder and set up your component there.
And you will initialize your custom picker like this:
myPickerView = MyPickerView(frame: yourFrame)
just got some great help which made the UIPickerView wheels work perfectly but they I have been trying to get the data in the 2nd and 3rd components to change dependant on the position of the 1st component.
I can get through some println lines to work out that the variable that I pull out whatConversion has the correct value but I have no idea how to change the array and make the UIPickerView update with the new values.
Please help for my sanity and also I am going to have to put extra time in at work after spending nearly all day on these, what dozen lines of code.
Thanks in advance
Motty
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
#IBOutlet weak var picker1Label: UILabel!
#IBOutlet weak var picker2Label: UILabel!
#IBOutlet weak var bigPicker: UIPickerView!
var wheelContents:[[String]] = []
var length = ["metres","feet","yards","inches","mm","cm","miles"]
var volume = ["m3","US Gall","Imp Gall","Barrels", "cubic FT","litres"]
var conType = ["length","volume"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
bigPicker.delegate = self
wheelContents = [conType, length, length]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//For main selection of type of conversion
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(bigPicker: UIPickerView) -> Int{
return wheelContents.count
}
// returns the # of rows in each component..
func pickerView(bigPicker: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return wheelContents[component].count
}
func pickerView(bigPicker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
return wheelContents[component][row]
}
func pickerView(bigPicker: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var whatConversion = wheelContents[0][bigPicker.selectedRowInComponent(0)]
switch(whatConversion){
case "length":
wheelContents = [conType, length, length]
bigPicker.numberOfRowsInComponent(wheelContents[component].count)
break
case "volume":
wheelContents = [conType, volume, volume]
break
default:
break
}
}
}
I am even happier I managed to figure this out myself by looking at the Class definitions and functions
used bigPicker.reloadAllComponents()
Hi there I have been trying to get this working for ages with no success so apologies if this seems easy for you guys.
I want to use a 3 wheel UIPickerView - The 1st wheel didSelectRow value will be used to change the array for the remaining two wheels but they will be the same as it is a conversion app.
It seems to throw me an error up when populating the wheels saying that Anyobject is not identical to String. I cannot see anything wrong but I know it is something basic I have missed.
Any pointers would be much appreciated.
Motty.
// ViewController.swift
// picker test
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
#IBOutlet weak var picker1Label: UILabel!
#IBOutlet weak var picker2Label: UILabel!
#IBOutlet weak var bigPicker: UIPickerView!
var wheelContents = []
var length = ["metres","feet","yards","inches","mm","cm","miles"]
var volume = ["m3","US Gall","Imp Gall","Barrels", "cubic FT","litres"]
var conType = ["length","volume"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
wheelContents = [conType,length,length]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//For main selection of type of conversion
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(bigPicker: UIPickerView) -> Int{
return wheelContents.count
}
// returns the # of rows in each component..
func pickerView(bigPicker: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return wheelContents[component].count
}
func pickerView(bigPicker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
return wheelContents[component][row]
}
}
You need to tell Swift that your wheelContents array is an array of array of String:
var wheelContents:[[String]] = []
If you don't explicitly give wheelContents a type, Swift infers it to be NSArray which is what is giving you problems.
You need to say swift, that your wheelContents is an array of String arrays:
var wheelContents:[[String]] = []
Also you should set the delegate of your pickerView to self because you handle the delegate in your class. Otherwise the functions won't work properly:
//In the viewDidLoad method
bigPicker.delegate = self
Since you declared wheelContents like this wheelContents = [], without specifying its elements type, the compiler automatically infers that it is array of AnyObjects aka [AnyObject].
That's the reason why when you are returning wheelContents[component].count it generates an error: at that moment the compiler is expecting a String! but you are providing an AnyObject.
It's a really easy fix, you should just specify what the content of the array it is going to be when you declare it. (it's an array of arrays of strings aka [[String]])
You need to set the datasource of pickerView along with the delegate in viewDidLoad
pickerView.dataSource = self