what is the exact meaning of delegate in iphone?how it is implemented in UIViewController?
A delegate is an object that usually reacts to some event in another object and/or can affect how another object behaves. The objects work together for the greater good of completing a task. Typically a delegate object will be shared by many other objects that have a more specific task to carry out. The delegate itself will be more abstract and should be very reusable for different tasks. The object which contains the delegate typically sends the delegate a message when a triggered event occurs, giving the delegate an opportunity to carry out its specified task.
There's more documentation here that you should read to understand the delegate pattern in Cocoa and Cocoa touch, particularly with how delegation is used between UIWindow and UIView. It is an integral design pattern in iPhone architecture and one that should be mastered if you wish to design a clean application.
Delegates are used to delegate tasks of objects to their owner (or any object, really). A good reason for this is it makes it easier to use composition instead of inheritance. Delegates are a reference to an object that conform to a specified protocol so you can guarantee it will implement the required methods. A good example is UIApplicationDelegate. Notice how the delegated methods (from the protocol) use verbs that applicationDid, applicationShould, applicationWill etc. Usually delegate methods either ask for permission to do something (and choose to do it this way, in a method, rather than with just a BOOL property, for more flexibility) or notify the delegate of an event that will happen or did happen.
Delegation is a pattern and the term means the same in Cocoa.
A delegate is not implemented in a UIViewController
. Different kinds of view controllers are assign
ed a delegate to handle certain tasks. One of the best examples (if we're talking iPhone) is the UITableViewDelegate
, which is called to do certain things when certain table-related events occur.
A delegate is used to communicate/pass data b/w two objects of a class/struct to complete a task. For example: Consider firstVC(sender or Delegator or CEO) which sends confidential data to secondVC(receiver or Delegate or Secretary). This is done by making secondVC conforming to a
protocol passDataDelegate { func passdata(data: String) }
class secondVC : UIViewController, passDataDelegate {
func passdata(data: String) {
print("CEO passed //(data)")
}
}
class firstVC : UIViewController {
var delegate : passDataDelegate?
}
Now create objects of both firstVC & secondVC
let sender = firstVC()
let receiver = secondVC()
Since, receiver conforms to protocol passDataDelegate
. So, it is having type either as UIViewController
or passDataDelegate
because if a class conforms to a protocol then the object of its class can have protocol as type.
Hence, assign sender.delegate = receiver
Now, we can CEO(sender) can pass data to Secretary(receiver) through its delegate property as
sender.delegate?.passdata("Confidential data")
Output: CEO passed Confidential data
Now the Secretary(receiver) can use that data to complete her further task.
The concept of delegation is very common in iOS. An object will often rely on another object to help it out with certain tasks. This separation of concerns keeps the system simple as each object does only what it is good at and lets other objects take care of the rest. The table view offers a great example of this.
© 2022 - 2024 — McMap. All rights reserved.