/SwiftPickers

A set of easy to use data pickers that can be used by themsevles or displayed via an included button which handles displaying the selected values

Primary LanguageSwift

SwiftPickers

Language Platform License Issues

Overview

This is a Swift port of ActionSheetPicker-3.0. However, this project contains some functionality which is different from the original component and certain design decisions have been taken to keep things simpler for this version.

The SwiftPicker components can be used in one of two ways - you can either invoke the picker directly from code:

swift-pickers

Or, you can use the easy to use picker buttons which can be added to a storyboard or instantiated via. The buttons require a couple of lines to set up the picker and then the button will handle the rest:

picker-buttons

As you will note above, each button displays the current selection value as its title.

Installation

You can use the SwiftPicker components two ways:

Pickers

The basic picker components of SwiftPickers are not visual components that can be added via Interface Builder. Instead, you have to create them via code.

There are several different picker components depending on the type of value you want to display but one of the most basic picker components is the StringSwiftPicker, which allows you to pick a value from a list of string values passed to the picker.

Instantiating and invoking a StringSwiftPicker is as simple as:

let data = ["Red", "Blue", "Green", "Yellow"]
let p = StringSwiftPicker(title:"Colours", data:data, selected:0, done:{(pv, index, value) in
	println("Selected item: \(index) with value: \(value)")
}, cancel:{(pv) in
	println("Cancelled selection")
})
p.showPicker(self)

Note: Till the documentation is updated to reflect all functionality, refer to the ViewController.swift file in the included sample project for all the different ways that the various picker components can be invoked via code.

Button Pickers

The button picker components are the visual components in the SwiftPicker collection. They can be added to your application in two different ways:

Via Interface Builder

  • Add a standard UIButton instance to your view on your storyboard.
  • Change the class for the button to the SwiftPicker button class of your choice (ex: StringPickerButton, DatePickerButton etc.)
  • Set up an outlet for the button in your view controller and connect the button to the outlet.
  • Set up any configuration values for the button that can't be set via the storyboard in your code. You always have to pass a view controller instance to the button but the other configuration values vary depending on the type of picker button.

Via Code

  • Create an instance of a SwiftPicker button in your view controller:
let data = ["Red", "Blue", "Green", "Yellow"]
let btn = StringPickerButton(vc:self, title:"Colour Picker", data:data, selected:1, picked:{(val, ndx) in
	println("Picked the value: \(val) with index: \(ndx)")
})
  • Add the button to your view and you're done :)

Note: Till the documentation is updated to reflect all functionality, refer to the ButtonsViewController.swift file and the storyboard in the included sample project for all the different ways that the various button pickers can be used in a project.

Credits

  • The inspiration for SwiftPickers, ActionSheetPicker, was originally created by Tim Cinel (@TimCinel)
  • The version of the code that I used as the basis for this version of SwiftPickers was created by Petr Korolev

Questions?