Objective-C学习笔记 利用协议实现回调函数
Objective-C学习笔记 利用协议实现回调函数是本文要介绍的内容,主要是实现一个显示文字为测试的视图,然后经过3秒钟测试文字变为回调函数文字。相应的截图如下:
实现的代码如下:
定义协议:
#import <UIKit/UIKit.h> @protocol NoteDelegate //回调函数 -(void)messageCallBack:(NSString *)string; @end
调用协议:
#import <Foundation/Foundation.h> #import "NoteDelegate.h" @interface ManagerMessage : NSObject { id<NoteDelegate> *noteDelegate; } @property (nonatomic,retain) id<NoteDelegate> *noteDelegate; -(void)startThread; @end #import "ManagerMessage.h" @implementation ManagerMessage @synthesize noteDelegate; //开始一个线程 -(void)startThread { [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(targetMethod:) userInfo:nil repeats:NO]; } -(void)targetMethod:(NSString *)string { if (self.noteDelegate!=nil) { //完成线程 调用回调函数 [self.noteDelegate messageCallBack:@"回调函数"]; } } @end
前台页面实现:
相关推荐
lipin 2020-11-03
88254251 2020-11-01
VitaLemon 2020-10-15
PinkBean 2020-08-19
sfkong 2020-08-02
yanchuncheng 2020-07-30
doupoo 2020-07-28
bowean 2020-07-08
88520191 2020-07-05
Magicsoftware 2020-06-11
ChinaGuanq 2020-06-08
angqiuli 2020-06-06
whynotgonow 2020-06-06
whynotgonow 2020-06-03
陈旭阳 2020-06-02
airfling 2020-05-31
88520191 2020-05-20
RainyX 2020-05-19
89500297 2020-05-16