iOS Delegates

Here we will learn iOS delegates in swift with examples and how to use iOS delegates in swift to communicate with multiple objects using Xcode editor with example.

iOS Delegates

IOS delegate is the same concept of all programming languages which means that delegate is used for communication between multiple objects. You can also imagine that ios delegate is the simple way to connect objects and communicate with each other. In another word, we also say that delegates are basically used to send messages from one object to another objects.

 

Now if you want to make your National identity card you enter the office and go to the counter 1 it takes the basic information and passed to the counter 3 for further process completion. Now the counter 3 is your delegate that handles the details which is passed by counter 1. Now after that counter 3 done the process and passed the information to counter 1 for further process.

 

Now we will see how to use iOS delegates in applications with an example using Xcode editor.

Create iOS Delegate App in Xcode

To create a new app in iOS Xcode open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below. In the welcome window click on the second option “Create a new Xcode Project” or choose File à New à Project.

 

Xcode application to create ios project

 

After selecting “Create a new Xcode project” a new window will open in that we need to choose template.

 

The new Xcode window will contain several built-in app templates to implement the common types of iOS apps like page-based apps, tab-based apps, games, table-view apps, etc. These templates are having pre-configured interface and source code files. 

 

For this iOS hello world example, we will use the most basic template “Single View Application”. To select this one, Go to the iOS section in left side à select Application à In the main area of dialog select “Single View Application” and then click on the next button like as shown below.

 

Select single view application from ios xcode templates

 

After click Next we will get window like as shown below in this, we need to mention project name and other details for our application.

 

Product Name: “iOS Delegates”

 

The name whatever we enter in Product Name section will be used for the project and app.

 

Organization Name: “Tutlane”

 

You can enter the name of your organization or your own name or you can leave it as blank.

 

Organization Identifier: “com.developersociety”

 

Enter your organization identifier in case if you don't have any organization identifier enter com.example.

 

Bundle Identifier: This value will generate automatically based on the values we entered in the Product Name and Organization Identifier.

 

Language: “Swift”

 

Select language type as “Swift” because we are going to develop applications using swift.

 

Devices: “Universal”

 

Choose Devices options as Universal it means that one application is for all Apple devices in case if you have any specific requirement to run an app only for iPad then you can choose the iPad option to make your application restricted to run only on iPad devices.

 

Use Core Data: Unselected

 

This option is used for database operations. In case if you have any database related operations in your application select this option otherwise unselect the option.

 

Include Unit Tests: Unselected

 

In case if you need unit tests for your application then select this option otherwise unselect it.

 

Include UI Tests: Unselected

 

In case if you need UI tests for your application then select this option otherwise unselect it.

 

Once you finished entering all the options then click on Next button like as shown below

 

Enter Details to Create iOS Delegates Application in Xcode

 

Once we click on the Next button new dialog will open in that we need to select the location to save our project. Once you select the location to save project then click on Create button like as shown below.

 

Give path to save new ios application in xcode

 

After clicking on the Create button, the Xcode will create and open a new project. In our project, Main.storyboard and ViewController.swift are the main files that we used to design app user interface and to maintain source code. 

 

Main.storyboard – Its visual interface editor and we will use this file to design our app user interface 

 

ViewController.swift – It contains the source code of our application and we use this file to write any code related to our app.

 

Now in project select Main.storyboard file, the Xcode will open visual interface editor like as shown below.

 

iOS Delegates Application Main.StoryBoard File Interface

 

Now select ViewController.Swift file in your project that view will be like as shown below.

 

iOS Delegates Application View Controller Default Code in Xcode

Add iOS UI Controls to StoryBoard in Xcode

Now open Main.storyboard file and click in ViewController. Go to the Editor à Embed In à Navigation Controller like as shown below.

 

Add Navigation Controller to iOS Delegates Application

 

Now go to Object Library search for ViewController in Filter filed and drag the ViewController and drop into Main.storyboard like as shown below

 

Add viewcontroller to storyboard viewcontroller in ios delegates app

 

Same way drag the Bar Button Item from the object library and set the System Item “Add” when you select Add option the + button will appear like as shown below.

 

Add Bar Button Item to ViewController in iOS Swift Application

 

Now press Ctrl button in keyboard and drag “+” button and drop into third ViewController like as shown below

 

Add bar button to another viewcontroller in ios swift application

 

Once we make a connection between controllers our Main.storyboard file contains controllers relation like as shown below

 

ios delegates multiple controllers in storyboard file

 

Now we will add a new class file in our application for that Go to the File à New à File like as shown below.

 

Add New File in iOS Delegates Application

 

 Now select “Cocoa Class” and click Next like as shown below

 

Add new cocoa class in ios delegates application in xcode

 

Once we click Next we will get new dialogue in that Give the name as “LastNameViewController” and select “UIViewController” option for Subclass of to extend functionality, select Language “Swift” and click on next button to create a new class like as shown below.

 

Give name to new class in ios delegates application in xcode

 

Now click on third ViewController and set the LastNameViewController class like as shown below

 

Map Controller to New Class in iOS delegates application

 

Now go to the Object library drag the button and one text field and drop into the third view Controller and change the button name like as shown below

 

Add ui controls button, label to view controller in ios delegates application

Connect iOS UI Controls to Code in Xcode

Now open the Editor in Assistant mode to make the connection between UI controls (textfield, button) and LastNameViewController.swift file for that click the Assistant button in the Xcode toolbar near the top right corner of Xcode to open the assistant editor like as shown below

 

Assistant Editor in iOS Xcode to Mapp Controls with Code

 

Now press Ctrl button in keyboard and drag the controls from your canvas to the code display in LastNameViewController.swift file like as shown below.

 

Map ios ui controls with viewcontroller.swift file in xcode editor

 

Once we map controls to LastNameViewController.swift file we need to write code to perform button action and our LastNameViewController.swift file should contain code like as shown below

 

import UIKit

protocol DataEntertedDelegate {

func userDidEnterInofrmation(info:NSString)

}

class LastNameViewController: UIViewController {

@IBOutlet weak var textField: UITextField!

var delegate:DataEntertedDelegate?=nil

@IBAction func sendData(sender: AnyObject) {

if (delegate != nil) {

let information:NSString = textField.text!

delegate!.userDidEnterInofrmation(information)

self.navigationController?.popViewControllerAnimated(true)

}

}

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view.

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

Go to the Main.storyboard file clicks on Second View Controller drag the label from the object library and drop into the second ViewController then open the Editor in Assistant Mode and make the label connection to the ViewController.Swift file like as shown below

 

ios Add label control to viewcontroller and map control to code in xcode delegates application

 

Now click the connection between second ViewController and third ViewController and give the identifier name “Send” like as shown below.

 

Add connection between controllers in iOS delegates application

 

Once we finished all the required modifications our ViewController.swift file code will be like as shown below

 

//  ViewController.swift

//  iOS Delegates

//  Created by Tutlane on 13/08/2016.

//  Copyright © 2016 Tutlane.com. All rights reserved.

 

import UIKit

// import LastNameViewController

class ViewController: UIViewController,DataEntertedDelegate {

@IBOutlet weak var dataLabel: UILabel? = UILabel()

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

}

func userDidEnterInofrmation(info: NSString) {

dataLabel?.text = info asString

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "SendData" {

let secondVC:LastNameViewController = segue.destinationViewController as! LastNameViewController

secondVC.delegate = self

}

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

 Now we will run and see the output of the iOS Delegates application. To run application, first select required simulator (Here we selected iPhone 6s Plus) and click on Play button, located at the top-left corner of the Xcode toolbar like as shown below.

 

Run iOS Delegates Application By selecting simulator in Xcode

Output of iOS Delegates Application

Following is the result of the iOS Delegates application. Now run your application and click on + button icon when we click on “+” icon the third ViewController will open in that enter the name in a text field and click “ShowData” like as shown below

 

iOS Delegates Application Example Output or Result

 

Once the operation finished in the third viewcontroller then the data will send to the second viewcontroller using delegate like as shown below

 

Send to ViewController using Delegates in iOS Delegates Application

 

 This is how we can use iOS delegates in our applications to send data from one controller to another controller based on our requirements.