Delegates in Objective-c

It is common practice in lens c to use the controller as a delegate for various protocol implementations. Am I saying that when using the iOS SDK, or is it a good idea to have separate classes that take on the role of a delegate? Or is it just the case that best suits the scenario? I am mostly interested in learning about best practices in Objective-C since I am learning to code it in isolation and have no “real” expert.

+8
ios objective-c iphone cocoa-touch
source share
6 answers

The "controller" focuses on ownership and facade. External objects will talk to the controller, not to the controlled one. The only object that usually needs to talk directly to a UITableView is the UITableViewController . The controller typically owns the monitored object, and the controller must have at least the same lifespan as the monitored one.

The delegate focuses on behavior, strategy and observer. The object asks its delegate for guidance on behavior (should I show this data? What color should it be? Can I do this?). The object tells its delegate when interesting things happen (I was touched. I'm going to finish something. I had a problem.) The special delegate answers data questions (what data is on line 3?). They are called data sources.

In cases where the rest of the system is talking to the “controller”, it is usually not recommended to talk to the “delegate”. So, for example, it is usually advisable to have a pointer to the UITableViewController and send its messages from other places in the system. It is wrong to have a pointer to a tableView controller; You must work through the controller. On the other hand, if you have a pointer to an object, it is usually not recommended to request its delegate . If you need it, you have probably designed something wrong. (The most notable example is the [[NSApplication sharedApplication] delegate] , which you almost always have to talk to. AppDelegate is an application delegate, not a dump for global characters.)

If an object has a controller, the controller is almost always a delegate. To my rules, which you are talking about, when an object is both a controller and a delegate, then this is a controller.

It is possible that one object is the delegate of several things, especially if most of them are short-lived (for example, presenting warnings). It is not uncommon for a UIViewController delegate certain things.

+7
source share
  • Saving delegates as separate classes makes the code clean and portable. You can reuse these delegate classes for any other purpose by simply copying them and pasting them and making minor changes.

  • The advantage is to keep delegates in the same class for storing them in separate classes. You can easily access private variables / objects in the class, which is not possible in the first case.

  • If you keep delegates separately and want to pass some value to the main class, you may have to create another delegate :-) or add observers, or you must pass them using some properties.

+5
source share

Short answer: Yes, very often for the view controller there must be a delegate of the view (or several views) that it controls.

Examples: UITableViewController is a class for managing an instance of UITableView, and it implements both table view presentation protocols and data presentation table data sources (another type of delegate is a data source). Similarly, for a view controller, it would be natural to be a delegate to represent a selection, search string, text field, etc.

Rationale: Views should really not have information about where the data comes from, from which they are displayed, but view managers, so it’s natural to provide data and make decisions about how this view should behave. The same can be said for other kinds of objects, but you must use common sense. An application has its own delegation object, which is usually responsible for creating at least some view controllers and often data models, so it obviously does not make sense for the view controller to play this role.

+1
source share

Best practice depends on requirements as well as code. The most popular practice is MVC Pattern. You will get this idea as soon as you implement it. There are also some design patterns you can read. But for the iPhone, I prefer MVC. And for delegates, yes, it is normal practice in objective-c to use the controller as a delegate. objective-c is designed this way.

0
source share

If you plan on conveying information to several parts of your application, NSNotification is a good choice (even if it can make things a little difficult for others to understand). A.

In other cases, having a protocol-related delegate is a good choice to make things clear and isolated in terms of code.

KVC is also another way to implement interactions in your code: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html

Depending on what you plan to do, one solution may be better than another, it is best to play with each decision to find out more about them ... This will help you make the right decision when you develop your next iOS application!

0
source share

Here I go:

Suppose you have class A and another B.

"a" is an object of class A

In "a", if you create an instance of B as "b" and go to it self, that is, "a"

Now you can easily call the method of this variable "a" u, just passed in "b", because you have access to this object. But the chaos begins when you finish with class B and start releasing "b". Now, due to the use of “a,” all memory allocated to “b” will be released, but not freed, and will be confused until “a” is released. God will save you if you use a class B instance recursively.

Therefore, using a delegate, you can use all the necessary methods of class A and keep up with this problem.

0
source share

All Articles