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 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 handle 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 example using Xcode editor.
Create iOS Delegate App in Xcode
To create 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 welcome window click on the second option “Create a new Xcode Project” or choose File à New à 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 common type 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 most basic template “Single View Application”. To select this one, Go to the iOS section in left side à select Application à In main area of dialog select “Single View Application” and then click on next button like as shown below.
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 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 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
Once we click on 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.
After click on Create button the Xcode will create and open a new project. In our project Main.storyboard and ViewController.swift are the main files which 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 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.
Now select ViewController.Swift file in your project that view will be like as shown below.
Add iOS UI Controls to StoryBoard in Xcode
Now open Mai.storyboard file and click in ViewController. Go to the Editor à Embed In à Navigation Controller like as shown below.
Now go to Object Library search for ViewController in Filter filed and drag the ViewController and drop into Main.storyboard like as shown below
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.
Now press Ctrl button in keyboard and drag “+” button and drop into third ViewController like as shown below
Once we make connection between controllers our Main.storyboard file contains controllers relation like as shown below
Now we will add new class file in our application for that Go to the File à New à File like as shown below.
Now select “Cocoa Class” and click Next like as shown below
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.
Now click on third ViewController and set the LastNameViewController class like as shown below
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
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
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.
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 click on Second View Controller drag the label from 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
Now click the connection between second ViewController and third ViewController and give the identifier name “Send” like as shown below.
Once we finished all 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 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.
Output of iOS Delegates Application
Following is the result of 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 text field and click “ShowData” like as shown below
Once the operation finished in third viewcontroller then the data will send to second viewcontroller using delegate like as shown below
This is how we can use iOS delegates in our applications to send data from one controller to another controller based on our requirements.
No comments:
Post a Comment