zoukankan      html  css  js  c++  java
  • Objective-C学习笔记 利用协议实现回调函数

    来源:http://mobile.51cto.com/iphone-278354.htm

    Objective-C学习笔记 利用协议实现回调函数是本文要介绍的内容,主要是实现一个显示文字为测试的视图,然后经过3秒钟测试文字变为回调函数文字。相应的截图如下:

    Objective-C学习笔记 利用协议实现回调函数 

    Objective-C学习笔记 利用协议实现回调函数

    实现的代码如下:

    定义协议:

    #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 

    前台页面实现:

    #import "IphoneDeleteViewController.h"   
    #import "ManagerMessage.h"   
    @implementation IphoneDeleteViewController   
    @synthesize textView;   
     
    //回调函数   
    -(void)messageCallBack:(NSString *)string   
    {   
        self.textView.text=string;   
    }   
    - (void)viewDidLoad {   
        [super viewDidLoad];   
        self.textView.text=@"测试";   
        ManagerMessage *message=[[ManagerMessage alloc] init];   
        //通知调用协议   
        message.noteDelegate=self;   
        [message startThread];   
        [message release];   
    }   
    - (void)didReceiveMemoryWarning {   
        [super didReceiveMemoryWarning];   
    }   
    - (void)viewDidUnload {   
        self.textView=nil;   
    }   
    - (void)dealloc {   
        [self.textView release];   
        [super dealloc];   
    }   
    @end 

    小结:Objective-C学习笔记 利用协议实现回调函数的内容介绍完了,希望本文对你有所帮助。

  • 相关阅读:
    新线程 handler
    解决获取View的width和Height为0的4种方法
    回调深入理解 同步回调 以android中View.OnClickListener为列
    回调函数
    android:layout_weight
    studio rendering problems
    android:exported属性
    Codeforces 1264C/1265E Beautiful Mirrors with queries (概率期望、DP)
    Codeforces 1254C/1255F Point Ordering (交互题)
    Codeforces 576D Flights for Regular Customers (图论、矩阵乘法、Bitset)
  • 原文地址:https://www.cnblogs.com/sunminmin/p/3731610.html
Copyright © 2011-2022 走看看