I'm a complete beginner on iOS dev and I want to create a little iOS application. On this application, 3 UIPickerViews
are supposed to display different data.
My problem is on the display. I'm used to dev on Android or Windows phone and I don't understand how to populate the UIPickerViews
with different data.
This is the code I have already written:
//
// ViewController.swift
// iphoneVersion
//
// Created by fselva on 13/05/2015.
// Copyright (c) 2015 fselva. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate{
@IBOutlet weak var pickerView1: UIPickerView!
@IBOutlet weak var pickerView2: UIPickerView!
@IBOutlet weak var pickerView3: UIPickerView!
var test = ["Todo","Waiting","Maybe","Inbox","Note"]
var test2 = ["@Office","@Computer","@Home","@Meeting", "@Read", "@Achat", "@Call"]
override func viewDidLoad() {
super.viewDidLoad()
pickerView1.tag = 1
pickerView2.tag = 2
pickerView3.tag = 3
// 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.
}
func numberOfComponentsInPickerView(pickerView : UIPickerView!) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if pickerView2.tag == 2 {
return test.count
} else if pickerView3 == 3{
return test2.count
}
return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if pickerView2 == 2 {
return test[row]
} else if pickerView3 == 3{
return test2[row]
}
return ""
}
}
@IBOutlet weak var pickerView1: UIPickerView!
has been automatically created by Ctrl+Click from the storyboard to the ViewController.swift.
The first UIPickerView
is currently supposed to display nothing. The second one is supposed to display test
and the third one displays test2
.
After hours of investigation on internet, I have heard about tags to define who must display what, but it doesn't work.
Am I doing something wrong, am I missing something somewhere?
You don't need tags, just use:
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if pickerView == pickerView1 {
//pickerView1
} else if pickerView == pickerView2{
//pickerView2
}
also don't forget to set delegate in IB or in code:
pickerView1.delegate = self