Objective C terminology: outlets & delegates
Asked Answered
A

2

9

I'm having issues understanding the concept of outlets how the iPhone deals with events. Help! Delegates confuse me too. Would someone care to explain, please?

Abstracted answered 15/1, 2010 at 0:33 Comment(4)
not trying to be a jerk but have you read the introductory documents at developer.apple.com? Its just that the question would take an essay to answer. Maybe ask a more detailed question about what is hanging you up.Hanhhank
The documentation that Apple provides in the Cocoa Fundamentals Guide on outlets: developer.apple.com/iphone/library/documentation/Cocoa/… and delegates: developer.apple.com/iphone/library/documentation/Cocoa/… seems pretty thorough to me.Prodrome
@Brad - I'm not a documentation person.Abstracted
@BradLarson - Sorry, gotta redact that last one. It's been a while, but I've warmed up to iOS documentation.Abstracted
S
19

Outlets (in Interface Builder) are member variables in a class where objects in the designer are assigned when they are loaded at runtime. The IBOutlet macro (which is an empty #define) signals Interface Builder to recognise it as an outlet to show in the designer.

For example, if I drag out a button, then connect it to the aButton outlet (defined in my interface .h file), the loading of the NIB file at runtime will assign aButton the pointer to that UIButton instantiated by the NIB.

@interface MyViewController : UIViewController {
    UIButton *aButton;
}

@property(nonatomic, retain) IBOutlet UIButton *aButton;

@end

Then in the implementation:

@implementation MyViewController

@synthesize aButton; // Generate -aButton and -setAButton: messages

-(void)viewDidAppear {
    [aButton setText:@"Do Not Push. No, seriously!"];
}

@end

This eliminates the need to write code to instantiate and assign the GUI objects at runtime.


As for Delegates, they are event receiving objects used by another object (usually a generalised API class such as a table view). There's nothing inherently special about them. It's more of a design pattern. The delegate class may define several of the expected messages such as:

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

...and the API object calls this message on the delegate when it wants to notify it of the event. For example:

-(void)update:(double)time {
    if (completed) {
        [delegate process:self didComplete:totalTimeTaken];
    }
}

And the delegate defines the message:

-(void)process:(Process *)process didComplete:(double)totalTimeTaken {
    NSString *log = [NSString stringWithFormat:@"Process completed in %2.2f seconds.", totalTimeTaken];
    NSLog(log);
}

Such use might be:

Process *proc = [Process new];
[proc setDelegate:taskLogger];
[proc performTask:someTask];

// Output:
// "Process completed in 21.23 seconds."
Spireme answered 15/1, 2010 at 0:56 Comment(1)
So delegates are actually glorified versions of what other languages like Javascript or Python interpret as callbacks, correct?Goddess
F
9

A delegate is a object that another object can forward messages to. In other words, it's like when your mom told you to clean your room and you pawned it off on your little brother. Your little bro knows how to do the job (since you were too lazy to ever learn) and so he does it for you.

Fosque answered 15/1, 2010 at 1:9 Comment(1)
Why didn't mom ask little brother directly?Scrobiculate

© 2022 - 2024 — McMap. All rights reserved.