Let's start with a simple example.
Mom needs to buy food, wash clothes, cook and go to work every day. Mom wants her father to go to work. The Code is that Mom has four functions to buy food, wash clothes, and cook for work. Mom entrusts her father to go to work, so she wants to implement the function of going to work.
Create a delegate first
# Import <Foundation/Foundation. h> @ protocol setprotocol <nsobject> // go to work-(nsstring *) shangban; @ end
Create a mom class.
. H file
# Import <Foundation/Foundation. h> # import "setprotocol. H" @ interface MOTHER: nsobject // This attribute is used to specify the parent object. This object must implement the setprotocol protocol. @ Property (nonatomic, retain) ID <setprotocol> detegate; // maicai (nsstring *); // laundry-(nsstring *) xiyifu; // cooking-(nsstring *) zuofan; @ end
. M file
# Import "mother. H "@ implementation mother @ synthesize detegate = _ detegate; // buy food-(nsstring *) maicai {return @" buy food \ n ";} // wash clothes-(nsstring *) xiyifu {return @ "laundry \ n";} // cooking-(nsstring *) zuofan {return @ "cooking \ n";}-(nsstring *) shangban {return [_ detegate shangban];} @ end
Implement the father class
. H
#import <Foundation/Foundation.h>#import "setProtocol.h"@interface father : NSObject<setProtocol>@end
. M
# Import "father. H" @ implementation father-(nsstring *) shangban {return @ "work" ;}@ end
Csdn