Tech notes for ios

Swift 3 and iOS: UIPicker

In your view controller, use these delegates UIPickerViewDelegate, UIPickerViewDataSource. And implement the methods as follows:

public func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1

public func pickerView(_ pickerView: UIPickerView,
                       numberOfRowsInComponent component: Int) -> Int {
    return items.count

public func pickerView(_ pickerView: UIPickerView,
                       titleForRow row: Int,
                       forComponent component: Int) -> String? {
    return items[row]

The first method says the number of 'components' we have in each picker row. The second gives the items in our picker. And the final one gives the string for the ui picker. The items element here is a simple list in swift.

Finally add the picker to your storyboard, control drag it to the icon that represents the view controller, and make the view controller both the delegate and the data source for the picker.

If you want to detect when there's a change in the picker, use this method in your view controller:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    // Do something with the row
swift ios ios-uipicker

Swift 3 and iOS: Using a UITableView

First create a normal UIViewController. It could be a UITableViewController, but by using the UITableViewDataSource and UITableViewDelegate protocols we achieve the desired effect:

class ATableViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 3

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)

        cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"

        return cell

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Section \(section)"


The first method is the number of section headers in the table view. The second is the rows in each section. And the fourth returns the title for each of our three sections.

The third method says: "Get a named cell for the row we want". We then give that some text and finally return that.

Next create a UIViewController in your storyboard. Give it a custom class as the one you just created above. Then to its view add a UITableView. Give that auto layout margins to match its parent if you're using auto layout.

Within that UITableView add a UITableViewCell and give that a Restoration Identifier as LabelCell, as referenced above.

From this point, once you goto that controller, you should see your table view.

ios ios-uiviewtable swift

Swift 3 and iOS: Add a loading indicator to a UIButton

You can show a loading spinner, a UIActivityIndicatorView, within a UIButton, that can be showed and hidden and also changes the button's alpha and interactivity.

First create an extension, then add a UIActivityIndicatorView to the button, half its alpha value and disable the button on loading. And then do the opposite when we stop the loading:

extension UIButton {
    func loadingIndicator(_ show: Bool) {
        let tag = 808404
        if show {
            self.isEnabled = false
            self.alpha = 0.5
            let indicator = UIActivityIndicatorView()
            let buttonHeight = self.bounds.size.height
            let buttonWidth = self.bounds.size.width
   = CGPoint(x: buttonWidth/2, y: buttonHeight/2)
            indicator.tag = tag
        } else {
            self.isEnabled = true
            self.alpha = 1.0
            if let indicator = self.viewWithTag(tag) as? UIActivityIndicatorView {

Stolen from [here] (

ios swift ios-uibutton

Swift 3 and iOS: Send a Form URL encoded POST

Let's create a URLRequest, set its method, and then specify it to be a urlencoded.

var r  = URLRequest(url: URL(string: u)!)
r.httpMethod = "POST"
r.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let d = "somekey=someval&another=another".data(using:String.Encoding.ascii, allowLossyConversion: false)
r.httpBody = d

After we do that, we use the data method of the string to encode it as Data, and set the requests body as such. From that point we can use the request.

We'll use the URLSession's dataTask to create a task from all that.

let task = URLSession.shared.dataTask(with: r) { data, response, error in

Finall we call task.resume().

swift ios

Swift 3 and iOS: Go to a new UIViewController

Let's say you have a UIViewController. And another, separate Storyboard, named NewSB. And, in that, another UIViewController, also named newUIC.

You can present that from your current view controller with:

let sb = UIStoryboard(name:"NewSB", bundle: nil)
let uis = sb.instantiateViewController(withIdentifier: "NewUIC")
self.present(uis, animated: true, completion: nil)

The name of the view controller is set in Storyboard ID panel in the identity inspector. NewSB is just the filename of the storyboard.

swift ios

Page 4 of 7
prev next
Click me