如何使用代理从另一个控制器发送消息?

时间:2012-03-26 13:17:58

标签: ios uiviewcontroller delegates

我的一个控制器中有一个UDPSocket,我知道我必须使用代理。我必须添加哪些功能才能从另一个控制器访问我的udpsocket?

使用像

这样的smth会很棒
[controller1.udpsocket sendData:data toHost:host] 

但我知道它不好,该怎么做而不是它?

1 个答案:

答案 0 :(得分:1)

#import <Foundation/Foundation.h>

@protocol DownLoadImageDelegate<NSObject>
@required
-(void)ImageDownLoaded:(UIImage*)image;

@end


@interface DownLoadImage : NSObject 
{

    id<DownLoadImageDelegate> mDelegate;
    NSURLConnection *mConnection;
    NSMutableData *mData;
}

@property(nonatomic,retain) id<DownLoadImageDelegate> delegate;
@end

//InViewController class 

DownLoadImage *imageDownLoad = [[DownLoadImage alloc]init];
imageDownLoad.delegate=self;
//and implement the delegate method declared in DownLoadImage class in ViewController class as

-(void)ImageDownLoaded:(UIImage *)image
{
    NSLog(@"Image%@",image);
    [mImageView setImage:image];
}