//protocol definitions for delegates@protocolUpdatedelegate <nsobject>-(void) Update;@end@interfaceTest:nsobject//delegate Variable definition@property (nonatomic, weak)ID<UpdateDelegate>Delegate;//Blocktypedefvoid(^updateblock) (IDobj); @property (nonatomic, copy) Updateblock Updateblock;- (void) Starttest; @end@implementationTest- (void) starttest{[Nstimer scheduledtimerwithtimeinterval:3.0ftarget:self selector: @selector (scheduledend) Userinfo:nil repeats:no];}- (void) scheduledend{[self.DelegateUPDATE];//Delegate//Block if(Self.updateblock) {Self.updateblock (@"Test"); }}@end//Implement- (void) viewdidload{[Super Viewdidload]; Test*test =[[Test alloc] init]; Test.Delegate= self;//set up a delegate instance//Implement blockTest.updateblock = ^ (IDobj) {Uialertview*alert=[[uialertview Alloc] Initwithtitle:@"Tips"Message:objDelegate: Self Cancelbuttontitle:nil otherbuttontitles:@"Determine", nil]; Alert.alertviewstyle=Uialertviewstyledefault; [Alert show]; }; [Test starttest];//Start Timer}//The delegate object implements the method of the protocol declaration, which is called by the delegate object- (void) update{Uialertview*alert=[[uialertview Alloc] Initwithtitle:@"Tips"Message@"Time to" Delegate: Self Cancelbuttontitle:nil otherbuttontitles:@"Determine", nil]; Alert.alertviewstyle=Uialertviewstyledefault; [Alert show];}
iOS delegate and block