MapKit

declare the MapPoint conforms to MKAnnotation.

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/Mapkit.h>

@interface MapPoint : NSObject <MKAnnotation>
{
    NSString *title;
    CLLocationCoordinate2D coordinate;
}

//a new designated initializer for instances of MapPoint
- (void) initWithCoordiate:(CLLocationCoordinate2D) c title:(NSString *)t;

//This is a required property from MKAnnotation
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;

//This is an optional property from MKAnnotation
@property (nonatomic, copy) NSString *title;

@end

The protocol defines coordinate as a read-only property, which means there is a method named coordinate that returns a CLLocationCoordinate2D. If MapPoint is to conform to the MkAnnotation protocol, it must implement coordinate.

dealloc

- (void) dealloc
{
    [title release];
    [super dealloc];
}

Note that you don't release coordinate in the dealloc method because it is not an objective-c object and can't receive messages. The CLLocationCoordinate2D structure's memory will live inside each instance of MapPoint, and it will be created and destoryed automatically along with the object.

The protocol defines the required coordinate as a read-only property, which means there must be a method named coordinate that returns a CLLocationCoordinate2D, but it doesn't have to be a property in the class declaration.

The MKAnnotation protocol, like all protocols, only dictates method signatures. as long as the signatures match exactly, the conforming class can implement them however it wants with whatever instance variables it chooses.

 

We can think of a protocol as a contract. for example, MKAnnotationView has a annotation property declared as

@property (nonatomic, retain) id <MKAnnotation> annotation;

This declaration says that the annotation can be of any type(id), as long as it conforms to the MKAnnotation protocol(<MKAnnotation>). Therefore, the MKAnnotationView will only send messages from the MKAnnotation protocol to its annotation; it won't make any assumptions about the other messages that object might respond to.

UITextField and UITextFieldDelegate

a UITextField is also a responder: it is a direct subclass of UIControl, which is a subclass of UIView, which is a subclass of UIResponder. When a UITextField is tapped, it handles this event by becoming the first responder.

When a UITextField becomes the first responder, a keyboard appear on the screen. To remove the keyboard from the screen, you tell the UITextField to give up the first responder status by sending it the message resignFirstResponder. Once the first responder of the window is no longer a UITextField, the keyboard will disappear.

 

 

posted on 2012-06-13 22:56  grep  阅读(221)  评论(0编辑  收藏  举报