iOS Swift Basics Tutorial: Protocols and Delegates

Here a cool tutorial protocols and delegates using Swift. This link will explain more details about the delegate, https://www.andrewcbancroft.com/2015/03/26/what-is-delegation-a-swift-developers-guide/.

Getting started.

Before we start this tutorial, make sure you already setup your machine (mac) with these tools:

  1. Xcode 7.1: https://developer.apple.com/xcode/

Open Xcode and create a new Single View Application. For product name, use RevivalxSwiftProtocolDelegate and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Single View Application

Single View Application

RevivalxSwiftProtocolDelegate

RevivalxSwiftProtocolDelegate

Go to Main.Storyboard and set to Compact Width | Any Height.

Compact Width | Any Height

Compact Width | Any Height

Select View Controller in storyboard and select Editor > Embed In > Navigation Controller.

Embed in Navigation Controller.

Embed in Navigation Controller.

Navigation Controller.

Navigation Controller.

Go to Object Library and drag/drop a new View Controller into storyboard.

Main.Storyboard.

Main.Storyboard.

Go to Object Library again and drag/drop Bar Button Item into first view controller. Change the Bar Button Item icon to plus icon. Then press control button and at the same time drag the mouse/pointer to second view controller. Select show under Action Segue. Finally add Navigation Item into second view controller and name it for both view controllers.

Main.Storyboard.

Main.Storyboard.

Drag and drop Label and TextField from Object Library into both view controllers. Make sure it similar with example below. Don’t forget to set autolayout for each label and textfield. If you not really understand about the autolayout, feel free to read this tutorial. http://www.raywenderlich.com/83129/beginning-auto-layout-tutorial-swift-part-1

Main.Storyboard.

Main.Storyboard.

Create a new ViewController.swift (UIViewController).

Xcode 6.4 – New File.

Xcode 6.4 – New File.

Cocoa Touch Class

Cocoa Touch Class

UIViewController

UIViewController

Project navigator.

Project navigator.

Select second view controller and make sure you attach SecondViewController into each controllers in Identity inspector.

Identity inspector -UIViewController

Identity inspector -UIViewController

Select the Assistant Editor and open SecondViewController.swift. Ctrl and drag from the Text Field to the ViewController class to create the following outlet.

TextField outlet.

TextField outlet.

Ctrl and drag from the Button and create the following action.

Button outlet.

Button outlet.

Open ViewController.swift. Ctrl and drag from the Label to the ViewController class to create the following outlet.

Label outlet.

Label outlet.

Select Attribute inspector and name it as SecondSegue in Identifier field.

Storyboard segue.

Storyboard segue.

Go to the SecondViewController.swift file and replace with this code.

import UIKit

protocol DataEnteredDelegate{
    func userDidEnterInformation(info: NSString)
}

class SecondViewController: UIViewController {

    @IBOutlet weak var txtData: UITextField!
    var delegate:DataEnteredDelegate? = nil
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    @IBAction func sendData(sender: AnyObject) {
        if (delegate != nil){
            let data: NSString = txtData.text
            
            delegate!.userDidEnterInformation(data)
            self.navigationController?.popViewControllerAnimated(true)
        }
    }
}

Open ViewController.swift and update the with this code.

import UIKit

class ViewController: UIViewController, DataEnteredDelegate {

    @IBOutlet weak var lblResult: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    func userDidEnterInformation(info: NSString) {
        lblResult.text = info as String
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "SecondSegue" {
            let secondViewController: SecondViewController = segue.destinationViewController as! SecondViewController
            secondViewController.delegate = self
        }
    }
}

Run your project. Feel free to check out my source code here, https://github.com/datomnurdin/RevivalxSwiftProtocolDelegate.

Leave a Reply

Your email address will not be published. Required fields are marked *