程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言入門知識 >> ObjectC 實現自定義Event

ObjectC 實現自定義Event

編輯:C語言入門知識

跨類的通訊,可以使用Notification,也可以使用Event。這裡記錄一下實現Event的方法:
首先,定義的類要從UIControl繼承。

SocketUtils類 子control

SocketUtils頭文件


#import 
#import "AsyncSocket.h"

@interface SocketUtils : UIControl{

}

enum {
    UIControlEventAlert=0x00000001<<25, //可以自定義的從24 到27
    UIControlEventError=0x00000001<<26
};

SocketUtils.m文件

觸發事件的方法:

[self sendActionsForControlEvents:UIControlEventAlert];

使用SocketUtils的目標類

頭文件

#import 
#import "WasherSocketUtils.h"
@interface Head : UIView
@property (nonatomic,strong)SocketUtils* SocketUtils;

m文件

#import "Head.h"

@implementation Head
@synthesize SocketUtils=_SocketUtils;
-(id)init{
    _SocketUtils=[[SocketUtils alloc]init];
    ///注冊事件,這句是關鍵
    [_SocketUtils addTarget:self action:@selector(socketEvent:) forControlEvents:UIControlEventAlert];

    return [super init];
}
-(void)socketEvent:(SocketUtils*)paramSender{
    NSLog(@"event by xie:%@",paramSender);
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved