Delegates

http://www.systeen.com/2016/11/26/use-protocols-delegates-segues-swift-3/

I researched a bit more on delegates from this website and also used what I learned in class to write this blog. The website had very simple examples that I will take information from in this blog. This website and my knowledge gained from class are the base of what I write in this blog. I do not take credit for the examples I use in this blog.

A delegate is someone that does something on behalf of someone else. In coding language, a view controller(VCA) will be using information sent from(usually) another view controller(VCB). First of all you are going to need to set up a protocol for the delegate. A protocol is used to define functionality. This should probably be in the view controller you are sending information from(VCB). This will usually be a detail view controller or a picker, and the view controller receiving the data will usually be a normal view controller or a table view controller.  The protocol will have a name and a body. The name should be named after the view controller you are establishing it in(VCB), with the addition of the words protocol, or delegate at the end. The word Protocol would explain that the delegate(VCA) is the ViewController conforming to the protocol of the sending ViewController(VCB). The word delegate would explain that the delegate(VCA) is the delegate of the sending ViewController(VCB). The body of the protocol is going to be a function with an argument that contains the information we want to send to the delegate. VCA will need to conform to the protocol by having the name of the protocol after its class declaration, and also it must have the body of the protocol, the function mentioned before, somewhere in the body of its class.

After all this we need to focus on the VCB instead of its delegate(VCA). In VCB we need to make a variable “delegate” and set it equal to its delegate(written as “VCBDelegate”, but it is representing VCA). Then we are going to send the delegate information by typing “delegate?.” this code says that we are sending information to the VC that is the delegate(? means if a delegate has been established). Then we are going to write the body of the protocol which would be: “functionName(argument: info goes here)”. The function name is going to be something that describes what kind of information you are receiving, and the argument is going to be the information you are receiving inside of a container(a constant or variable). After this there might be something to close VCB view.

Now that this is done we can finish editing the delegate(VCA). In this ViewController we go to the function that is the body of the protocol. In this function we will pass the information we created in VCB. This will fulfill the argument inside the protocol’s function. After this we need to use a prepare for segue function. This is used to change the views from the receiving VCA to VCB. Although this code is being written last, VCB would not be able run without this code. The segue is a path changes the view from VBA to VCB in the first place. In this segue function we set the destination to VCB and we are setting VCB’s delegate to VCA. The code could look like “let viewControllerB(this is just a container it could be named anything) = segue.destination as! VCB” and “VCBDelegate = self”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s