Examples of Delegates in Swift [closed]

2019-01-01 13:34发布

I have been trying to learn how delegation with protocols work. I understood everything, but I can't think of when to use delegation other than when using table views and possibly scroll views.

In general, when is delegation used?

4条回答
浪荡孟婆
2楼-- · 2019-01-01 13:43

Delegation in IOS world and mostly in MVC (Model View Controller) is a way for the View to talk to the Controller and it's called "blind communication" and delegation means to give the " leading stick " to another object ( doesn't really care who is taking over but usually the Controller) to control over components that the view can not control on it's own (remember it's only a view) or doesn't own to make it more simple ....
the controller can talk to a view but the view can not talk to the controller without Delegation

查看更多
刘海飞了
3楼-- · 2019-01-01 13:47

Delegate Method to Selectionimages

Create baseClass And Insert the following code

enter image description here

Create Another class then insert code

enter image description here

enter image description here

查看更多
皆成旧梦
4楼-- · 2019-01-01 14:01

Delegation is used when you want to pass some information or state of object A to another object B. Usually object B is the object that created object A.

I will list some situations where you would use delegation.

  1. Yes you're right. table views and scroll views use delegates because they want to tell whoever is interested (usuall your view controller) that "someone selected a row!" or "someone scrolled the scroll view!". Not only do scroll views and table views use delegates, UITextField and UIDatePicker and a lot of other views use delegates too!

  2. View Controllers also have delegates. For example, UIImagePickerController. The reason why is roughly the same as above - because the UIImagePickerController wants to tell you messages like "an image has been selected!". Another example would be UIPopoverControllerDelegate. This delegate tells you things like "the popover has been dismissed!"

  3. Other classes that use delegates include CLLocationManager. This delegate tells you things like "the user's location has been detected" or "failed to detect the user's location".

  4. You can use delegation in your code when a certain view controller of yours wants to send messages to other view controllers. If it is a settings view controller, it might send messages like "the font size setting has been changed!" and the view controller that cares about the font size setting changing will know and change the font size of a label or something.

查看更多
明月照影归
5楼-- · 2019-01-01 14:06

What is Delegation?

First of all, you should know that Delegation Pattern is not exclusive for iOS world:

In software engineering, the delegation pattern is a design pattern in object-oriented programming that allows object composition to achieve the same code reuse as inheritance.

But working with delegation in the iOS world is so common, I assume that you can see many of classes that provide a delegation/datasource for giving the ability to provide properties or behaviors for the used instance. It is one of main mechanisms of how objects talk to each other in CocoaTouch.


Alternatives:

However, delegation is not the only way to let objects talk to each other in iOS, you might want to know that there are:

Remark: in case if you are interested in comparing between them, you might want to check the following articles:


When to use Delegation?

So, the question is: "So why should I use delegation instead of those options?"

I will try to make it simple; I would suggest the use of delegation when you have one to one relationship between two objects. Just to make it clearer, the goal of talking a little bit about the NotificationCenter is to try to make sense when delegations are used:

NotificationCenter represents one to many relationship; Simply, it works as: posting (notifying) a notification on a specific event and observing (get notified about) this notification -- it could be observed anywhere else; Logically, that's what one to many relationship means. It is a representation of the Observer Pattern.


How to Apply Delegation?

For the purpose of simplifying, I would mention it as steps:

  1. Knowing the requirements: Each delegate has its own rules, listed in the delegate protocol which is a set of method signatures that you should implement for conforming this delegation.

  2. Conforming for the delegation: it is simply letting your class to be a delegate, by marking it. For instance: class ViewController: UIViewController, UITableViewDelegate {}.

  3. Connecting the delegate object: Marking your class to be a delegate is not enough, you need to make sure that the object you want to be confirmed by your class to give the required job to your class.

  4. Implementing the requirements: Finally, your class have to implement all required methods listed in the delegate protocol.


For Example

Does it sounds a little confusing? What about a real-world example?

Consider the following scenario:

Imagine that you are building an application related to playing audios. Some of the viewControllers should have a view of an audio player. In the simplest case, we assume that it should have a play/pause button and another button for, let's say, showing a playlist somehow, regardless of how it may look like.

So far so good, the audio player view has its separated UIView class and .xib file; it should be added as a subview in any desired viewController.

Now, how can you add functionality to both of the buttons for each viewController? You might think: "Simply, I will add an IBAction in the view class and that's it", at first look, it might sound ok, but after re-thinking a little bit, you will realize that it will not be applicable if you are trying to handle the event of tapping the button at the controller layer; To make it clear, what if each viewController implemented different functionality when tapping the buttons in the audio player view? For example: tapping the playlist in "A" viewController will display a tableView, but tapping it in the "B" viewController will display a picker.

Well, let's apply Delegation to this issue:

The "#" comments represents the steps of "How to Apply Delegation?" section.

Audio Player View:

// # 1: here is the protocol for creating the delegation
protocol AudioPlayerDelegate: class {
    func playPauseDidTap()
    func playlistDidTap()
}

class AudioPlayerView: UIView {
    //MARK:- IBOutlets
    @IBOutlet weak private var btnPlayPause: UIButton!
    @IBOutlet weak private var btnPlaylist: UIButton!

    // MARK:- Delegate
    weak var delegate: AudioPlayerDelegate?

    // IBActions
    @IBAction private func playPauseTapped(_ sender: AnyObject) {
        delegate?.playPauseDidTap()
    }

    @IBAction private func playlistTapped(_ sender: AnyObject) {
        delegate?.playlistDidTap()
    }
}

View Controller:

class ViewController: UIViewController {
    var audioPlayer: AudioPlayerView?

    // MARK:- Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        audioPlayer = AudioPlayerView()
        // # 3: the "AudioPlayerView" instance delegate will implemented by my class "ViewController"
        audioPlayer?.delegate = self
    }
}

// # 2: "ViewController" will implement "AudioPlayerDelegate":
extension ViewController: AudioPlayerDelegate {
    // # 4: "ViewController" implements "AudioPlayerDelegate" requirments:
    func playPauseDidTap() {
        print("play/pause tapped!!")
    }

    func playlistDidTap() {
        // note that is should do a different behavior in each viewController...
        print("list tapped!!")
    }
}


Quick Tip:

As one of the most popular examples of using delegation is Passing Data Back between View Controllers.

查看更多
登录 后发表回答