What is AppDelegates in Objective-C?

I am working on an iPhone tutorial ( link text , and it forced me to enter some code (a several times in various tutorials), but this does not explain it at all. In this code:

todoAppDelegate *appDelegate = (todoAppDelegate *)[[UIApplication sharedApplication] delegate]; 

What is appDelegate? What does “delegate” mean at the end of an instantiation? Actually, what does this mean? (UIAapplication sharedApplication)?

I am a .Net programmer if this helps someone explain it better. I hate learning through textbooks because I always need to know what EVERYTHING does, and no one explains everything.

+4
source share
3 answers

Let's go back a little.

Square brackets ( [ ] ) are the syntax for invoking the Objective-C method. Therefore, if Cocoa had C # syntax, the equivalent syntax would be as follows:

 TodoAppDelegate appDelegate = UIApplication.sharedApplication.delegate; 

In C #, you should use a static class for a class that has only one instance. Cocoa uses the Singleton pattern to achieve this. The class method (in this case, sharedApplication ) is used to retrieve a single instance of this class.

Delegates in Cocoa are not like the delegate keyword in C #, so don't confuse this. In C #, the delegate keyword is used to refer to a method. Cocoa's delegate template is provided as an alternative to the subclass.

Many objects allow you to specify another object as a delegate. Delegates implement the methods that these objects will call to notify about specific events. In this case, UIApplication is the class that represents the currently running application (for example, similar to System.Windows.Forms.Application ). It sends messages to its delegate when events occur that affect the application (for example, when the application starts, quits, receives or loses focus, etc.)

Another concept of Objective-C is protocol . In principle, this is similar to the .NET interface , except that methods can be marked as @optional , which means that classes are not required to implement methods marked in this way. The delegates in the iPhone SDK are simply objects that conform to a particular protocol. In the case of UIApplication , protocol delegates must conform to UIApplicationDelegate .

Since this is not required to implement each method, it gives the delegate the flexibility to choose the methods that are worth implementing. If you want, for example, to perform some actions when the application finishes launching, you can implement the class that conforms to the UIApplicationDelegate protocol, set it as an instance of the UIApplication delegate , and then implement applicationDidFinishLaunching:

UIApplication will determine if its delegate implements this method when the application finishes launching, and if so, call this method. This gives you the ability to respond to this event without subclassing UIApplication .

In iPhone applications, developers also often use the application delegate as a kind of top-level object. Since you usually don’t use the UIApplication subclass, most developers save their global application data in the application’s split.

+18
source

A delegate is simply an object that implements certain methods (mainly callbacks). NSApplication docs explains what his delegate should do and what messages he needs to reply to .

And this is not an instantiation. The above snippet does not create anything. It refers to any object specified as an application delegate. [UIApplication sharedApplication] gets the object representing the application, and sending the delegate to the application receives its delegate (if any).

+4
source

to add more to the combination of answers and another point of view, delegates are objects that can (but not necessarily) work on another object.

So, let's say you have objectA, and you can assign a delegate to it (let it delegate the object).

From the point of view of the facility, there are certain parts of the work that may be required. The actual work and the results of such work may be different depending on the context.

So, instead of having objectA implementing the method for all these instances, we say ... let another object, delegateObject, do the job ... and as long as the results are returned in the proper format, we don’t care what we did delegate Object to get there.

objectA will first verify that object delegation exists, and that delegateObject has implemented a method for completing tasks.

For this, NSObject (which inherits every Cocoa object) has this method:

 - (BOOL)respondsToSelector:(SEL)aSelector 

and objectA will run a simple test before sending a message to delegate the object, for example:

 if ([delegate respondsToSelector: @selector(someMethod:sender:)]) { [delegate someMethod:@"stuff" sender:self]; } 

and since objectA only sends a message to its delegate, if assigned, the delegate is not saved by object.

If we were to use UITableView as an example, it has many UITableViewDelegate methods. One of them:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

every time the user touches a row in the table, the UITableView object first checks to see if there is a delegate, if there is a delegate, it will check that the delegate has executed the above method. If so, he will send a message to the delegate. This method does not expect a return value, and the UITableView will go its own fun no matter what the delegate does. And if there is no delegate that implements this method, then nothing happens.

+2
source

All Articles