B
B
Bullstreak2014-07-23 15:26:07
Objective-C
Bullstreak, 2014-07-23 15:26:07

What are selectors and delegates in Objective-C?

I'm learning objective c. Reached the chapter "Callback" (a book by Aaron Hillegas, translated into Russian).
There are three callback models (Action Receiver, Helper Objects, and Alert Center).
Explain in detail event programming in objective c.
I already **** Xia to re-read the same chapter in the tutorial. On the Internet, I found only incoherent pieces of code, etc.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
deleted-mezhevikin, 2014-07-24
@deleted-mezhevikin

You are reading some complicated and unnecessary book, open the office. documentation and study by examples.
In simple words:
A selector is a method that will process some action
For example: we have a button and we specify a selector with the myButtonWasPressed method, this method will be called when the button is pressed.

[myButton addTarget:self
             action:@selector(myButtonWasPressed)
   forControlEvents:UIControlEventTouchUpInside];

- (void)myButtonWasPressed {
    // Do something about it
}

A delegate is when one class works inside another class. For example, we have a UITableView table and we want it to be displayed in the MainViewController controller, we set the MainViewController delegate for the table and now the delegate methods for building the table cell will be called in the MainViewController

V
Vanya Ivanov, 2015-01-04
@mr_cloud

Delegates - https://vk.com/videos-58860049?section=search&z=vi...
Selectors - https://vk.com/videos-58860049?section=search&z=vi...
ps: don't be afraid, Russian language

F
Fadmin, 2014-07-24
@Fadmin

try to watch the video on hexlet.org, there are definitely about delegates there.

N
Ne0nX, 2014-08-02
@Ne0nX

Read more about the Delegate pattern.
In Objective-C, it is based on the
delegate protocol that implements methods that the executor object cannot implement itself.
For example, let's take a UITableView and its Delegate, which must implement the required methods.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

without these methods from TableView there would be no point.
Selector - in simple terms, this is a kind of pointer to a method.
[myButton addTarget:self
             action:@selector(myButtonWasPressed:)
   forControlEvents:UIControlEventTouchUpInside];

With this code, we say that when the button is pressed, the myButtonWasPressed method will be called.
Delegates are very well written in the book "The Power of Objective-C 2.0".
I advise you to read it!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question