资料
delegate:
http://en.wikipedia.org/wiki/Delegation_pattern
Objective-C 2.0
// A custom view that scrolls its children.
@interface TCScrollView : NSView {
id delegate; // A delegate that wants to act on events in this view
}
-(IBAction)scrollToCenter:(id)sender; // A method that can be bound to a button in the UI
-(void)scrollToPoint:(NSPoint)to;
// Accessors. Implementation not shown.
@property (nonatomic, assign) id delegate;
@end
// This is a formal protocol: implementor doesn't have to implement all or even any of
// the optional methods in the protocol
@protocol TCScrollViewDelegate
@optional
-(BOOL)scrollView:(TCScrollView*)scrollView shouldScrollToPoint:(NSPoint)newPoint;
@end
@implementation TCScrollView
@synthesize delegate;
-(IBAction)scrollToCenter:(id)sender; {
[self scrollToPoint:NSPointMake(0,0)];
}
-(void)scrollToPoint:(NSPoint)to {
BOOL shouldScroll = YES;
// If we have a delegate, and that delegate indeed does implement our delegate method,
if(delegate && [delegate respondsToSelector:@selector(scrollView:shouldScrollToPoint:)])
shouldScroll = [delegate scrollView:self shouldScrollToPoint:to]; // ask it if it's okay to scroll to this point.
// If not, ignore the scroll request.
if(!shouldScroll)
return;
// Scrolling code omitted.
}
@end
@interface MyCoolAppController : NSObject <TCScrollViewDelegate> {
IBOutlet TCScrollView* scrollView;
}
@end
@implementation MyCoolAppController
-(void)awakeFromNib {
[scrollView setDelegate:self];
}
-(BOOL)scrollView:(TCScrollView*)scrollView shouldScrollToPoint:(NSPoint)newPoint {
if(newPoint.x > 0 && newPoint.y > 0)
return YES;
return NO;
}
@end
http://stackoverflow.com/questions/4516165/java-command-pattern-vs-iphone-delegate-pattern
delegate:Cocoa Fundamentals Guide.pdf(p183)
Implementing a Delegate for a Custom Class
To implement a delegate for your custom class, complete the following steps:
■ Declare the delegate accessor methods in your class header file.
- (id)delegate;
- (void)setDelegate:(id)newDelegate;
■ Implement the accessor methods. In a memory-managed program, to avoid retain cycles, the setter
method should not retain or copy your delegate.
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)newDelegate {
delegate = newDelegate;
}
In a garbage-collected environment, where retain cycles are not a problem, you should not make the
delegate a weak reference (by using the __weak type modifier). For more on retain cycles, see “Object
Ownership and Disposal” in Memory Management Programming Guide. For more on weak references in
garbage collection, see “Garbage Collection for Cocoa Essentials”.
■ Declare a formal or informal protocol containing the programmatic interface for the delegate. Informal
protocols are categories on the NSObject class. If you declare a formal protocol for your delegate, make
sure you mark groups of optional methods with the @optional directive.
“The Form of Delegation Messages” (page 180) gives advice for naming your own delegation methods.
■ Before invoking a delegation method make sure the delegate implements it by sending it a
respondsToSelector: message.
- (void)someMethod {
if ( [delegate respondsToSelector:@selector(operationShouldProceed)] ) {
if ( [delegate operationShouldProceed] ) {
// do something appropriate
}
}
}
The precaution is necessary only for optional methods in a formal protocol or methods of an informal
protocol.
1、Memory Management Programming Guide.pdf(p18)
Weak References to Objects
Retaining an object creates a “strong” reference to that object. An object cannot be deallocated until all of
its strong references are released. An object’s lifetime is thereby determined by the owners of its strong
references. In some cases, this behavior may not be desired. You may want to have a reference to an object
without preventing the object from deallocating itself. For these cases, you can obtain a “weak” reference.
A weak reference is created by storing a pointer to an object without retaining the object.
Weak references are essential in cases where a circular reference would otherwise be set up. For example, if
Object A and Object B communicate with each other, each needs a reference to the other. If each retains the
other, neither object ever gets deallocated until the connection is broken, but the connection is not broken
until one of the objects is deallocated. Catch-22. To break the circle, one object takes a subordinate role and
obtains a weak reference to the other. As a concrete example, in a view hierarchy, a parent view owns, and
hence retains, its child views, but a child view does not own its parent; the child still needs to know who its
parent is, so it keeps a weak reference to its parent.
Additional cases of weak references in Cocoa include, but are not restricted to, table data sources, outline
view items, notification observers, and miscellaneous targets and delegates.
Important: In Cocoa, references to table data sources, outline view items, notification observers, and delegates
are all considered weak (for example, an NSTableView object does not retain its data source and the
NSApplication object does not retain its delegate). The documentation only describes exceptions to this
convention.
You need to be careful about sending messages to objects for which you only hold a weak reference. If you
send a message to an object after it has been deallocated, your application will crash. You must have
well-defined conditions for when the object is valid. In most cases, the weak-referenced object is aware of
the other object’s weak reference to it, as is the case for circular references, and is responsible for notifying
the other object when it deallocates. For example, when you register an object with a notification center,
the notification center stores a weak reference to the object and sends messages to it when the appropriate
notifications are posted. When the object is deallocated, you need to unregister it with the notification center
to prevent the notification center from sending any further messages to the object, which no longer exists.
Likewise, when a delegate object is deallocated, you need to remove the delegate link by sending a
setDelegate: message with a nil argument to the other object. These messages are normally sent from
the object’s dealloc method
2、@property assign
http://stackoverflow.com/questions/1072541/i-have-a-circular-reference-how-can-i-create-a-weak-reference-in-objective-c
http://stackoverflow.com/questions/793224/how-can-i-make-a-weak-reference-to-a-parent-object-when-using-properties
3、addsubviews 与 superview
- (void)addSubview:(UIView *)view
Parameters
view
The view to be added. This view is retained by the receiver. After being added, this view appears on top of any other subviews.
Discussion
This method retains view and sets its next responder to the receiver, which is its new superview.
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.