Swift does not conform to protocol - ios

After pulling an app that was built using Swift 6 to now a system that is using 6 beta, I'm getting an "EventFormViewController does not conform to protocol UIPickerViewDataSource". I have struggled with this for several days now, any suggestions?
import UIKit
var eventChoices = [
["5","10","15","30","45","60","90","120","150","180"],
["Hospital Committee","Peer Review","EHR Improvement","Quality Improvement","Business Development"], ]
class EventFormViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate{
#IBOutlet weak var eventPicker: UIPickerView!
#IBOutlet weak var eventLabel: UILabel!
#IBOutlet weak var commentField: UITextField!
func updateLabel(){
let selectedTime = eventChoices[0][eventPicker.selectedRowInComponent(0)]
let event = eventChoices[1][eventPicker.selectedRowInComponent(1)]
eventLabel.text = "Chose \(event) for \(selectedTime) mins"
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
updateLabel() }
override func viewDidLoad() {
super.viewDidLoad()
}
// Do any additional setup after loading the view.
}
func didReceiveMemoryWarning() {
didReceiveMemoryWarning() // Dispose of any resources that can be recreated.
}
// returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return eventChoices.count }
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return eventChoices[component].count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return eventChoices[component][row] }
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
if (component == 0) {
return 50.0;
}
return 300.0;
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

There are alot of issues in your code.
1
The function header is commented.
// returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return eventChoices.count }
It should be like:
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return eventChoices.count
}
2
You are closing your class definition right after the viewDidLoad implementation.
It should be like:
import UIKit
var eventChoices = [
["5","10","15","30","45","60","90","120","150","180"],
["Hospital Committee","Peer Review","EHR Improvement","Quality Improvement","Business Development"], ]
class EventFormViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate
{
#IBOutlet weak var eventPicker: UIPickerView!
#IBOutlet weak var eventLabel: UILabel!
#IBOutlet weak var commentField: UITextField!
func updateLabel()
{
let selectedTime = eventChoices[0][eventPicker.selectedRowInComponent(0)]
let event = eventChoices[1][eventPicker.selectedRowInComponent(1)]
eventLabel.text = "Chose \(event) for \(selectedTime) mins"
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
updateLabel()
}
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated.
}
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return eventChoices.count
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return eventChoices[component].count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
{
return eventChoices[component][row]
}
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat
{
var width = 300.0
if (component == 0)
{
width = 50.0;
}
return width;
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}

You've got some typing-errors:
Before your didReceiveMemoryWarning you've got a close bracket which closes the class. So your other methods aren't in the class with the UIPickerViewDelegate and so the delegate thinks, that the methods doesn't exist.
So move the bracket to the end of your code to close the class.
Secondly, you've got a mistake after the didReceiveMemoryWarning method. You've got outcommented a method header:
// returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return eventChoices.count }
So change that to look like that:
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return eventChoices.count
}
And last but not least you need to override the didReceiveMemoryWarning method. So change that:
func didReceiveMemoryWarning() {
didReceiveMemoryWarning() // Dispose of any resources that can be recreated.
}
to that:
override func didReceiveMemoryWarning() {
didReceiveMemoryWarning() // Dispose of any resources that can be recreated.
}

Related

Determine Segue Based on picker selection IOS, swift

I have a picker with two options currently, and what I am looking for is, based on the selection made in the picker, to determine the next viewController below is what I tried to use however, I immediately got an error stating the last "if" statement should be converted into a let statement. Below is the code for my picker and attempt to lead into associated segue.
import UIKit
import Firebase
import CoreData
import CoreLocation
class SchoolPickerViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var SchoolPicker: UIPickerView!
let pickerData = ["Ohio State University", "University of Rochester"]
override func viewDidLoad() {
super.viewDidLoad()
SchoolPicker.dataSource = self
SchoolPicker.delegate = self
// Do any additional setup after loading the view.
}
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 pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
#IBAction func schoolSelectorGoButton(sender: AnyObject) {
if pickerData = "Ohio State University" {
performSegueWithIdentifier("ohioStateSelected", sender: sender)
}
if pickerData = "University of Rochester" {
performSegueWithIdentifier("rochesterSelected", sender: sender)
}
}
}
u should get selected picker data from Array
define
var selectedIndex = 0
then in this line
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
selectedIndex = row
return pickerData[row]
}
and in this action
#IBAction func schoolSelectorGoButton(sender: AnyObject) {
if pickerData[selectedIndex] == "Ohio State University" {
performSegueWithIdentifier("ohioStateSelected", sender: sender)
}
}

UIPickerView "didSelectRow" selection with button leading to url

I have a picker set up and want to have the user make a selection then tap a button which leads to a url. This is what I have so far:-
class PickerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var picker: UIPickerView!
#IBOutlet weak var label: UILabel!
var url = NSURL(string: "http://www.google.com")
var pickerData: [String] = [String]()
var pickerSites: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.picker.delegate = self
self.picker.delegate = self
pickerData = ["Google", "Facebook"]
pickerSites = ["http://www.google.com", "http://www.facebook.com"]
}
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) {
label.text = pickerSites[row]
}
siteBtn.addTarget(self, action: "didTapSite", forControlEvents: .TouchUpInside)
#IBAction func sitesBtn(sender: AnyObject) {
UIApplication.shared().openURL(NSURL(string: "http://www.google.com")!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I've connected the "siteBtn" to a button on the screen. The picker and text label were working fine until I added the code for the button. I'm not sure if I've put the button code in the correct place. I know that I need the button to connect with the picker "didSelectRow" and that the "pickerData" list needs to correlate to the "pickerSites" list but I'm stuck.
Any help would be much appreciated.
this is the minimal setup you need. feel free to ask if anything is unclear:
// outlet to the pickerview
#IBOutlet weak var picker: UIPickerView!
// datasource
let pickerData = ["Google", "Facebook"]
let pickerSites = ["http://www.google.com", "http://www.facebook.com"]
// pickerview datasource methods
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
// pickerview delegate method
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
// ibaction for button tap
#IBAction func goToURLButtonTapped(sender: UIButton) {
let selectedSite = pickerSites[picker.selectedRowInComponent(0)]
UIApplication.sharedApplication().openURL(NSURL(string: selectedSite)!)
}

How to navigate to a second viewController using a UIPickerview

How to navigate between two viewcontrollers using a UIPickerview
I am trying to navigate between two viewcontrollers using a UIPickerview, so i first learned how to use didSelectRow to change the title of a label and then i tried using some code to navigate to another viewcontroller but that didn't work.
The code that i used was:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet var label: UILabel!
#IBOutlet var pickerView: UIPickerView!
var food = ["hello", "hi", "hey"]
var placementAnswer = 0
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.
}
public func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return food.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return food[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
placementAnswer = row
}
#IBAction func labelChanged(sender: UIButton) {
if (placementAnswer == 0){
}else if (placementAnswer == 1){
let view2 =
self.storyboard?.instantiateViewControllerWithIdentifier("view2") as! ViewController2
self.navigationController?.pushViewController(view2, animated: true)
}else if (placementAnswer == 2){
}
}
}

Picker view does not return always the right value

I've defined a picker which does not return always the right value. Sometimes it just works but most of the time when I pick the item pickerView returns the wrong value.
I did associate the outlet delegate to the view controller and am using the following code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
var categories = ["animals", "profession", "sea_animals", "food", "tools", "misc", "all"]
var cat: String = ""
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.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
println("cat size: \(categories.count)")
return categories.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
println("cat: \(categories[row])")
return categories[row]
}
}
it works sometime but it is not reliable.
Any idea what I do wrongly?
Thanks!
I found the problem.
There is a function for selecting the row :)
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)

pickerView in Swift does not display data

This seems too simple to qualify as a post, but I've been stuck on this issue for longer than I'd like. The pickerView does not display the array [durations]. When run, the simulator is just the empty field. The code was copied directly from the UI class, yet it seems to be missing something.
class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var durationPicker: UIPickerView!
var durations = ["72 hours", "24 hours", "6 hours", "2 hours", "20 minutes"]
var durationsValue = [72.0, 24.0, 6.0, 2.0, 0.329]
var itemSelected: String = ""
var selectedDuration: Double = 0.0
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.
}
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 durations[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
itemSelected = durations[row]
selectedDuration = durationsValue[row]
}
}
You need to set the delegate and the datasource inside viewDidLoad()
durationPicker.delegate = self
durationPicker.dataSource = self
Or just connect your picker delegate and datasource using Xcode interface
I suggest watching this video. This lady does an excellent job explaining various iOS-related topics.
https://www.youtube.com/watch?v=MdXmIViD17U
The issue that jumps out at me is you've got arrays specified at the top. Rather than hard-code values in, you should use return yourArray.count to return Ints.

Resources