ios - 如何使用委托(delegate)从一个 Controller 向另一个 Controller 发送消息?

标签 ios uiviewcontroller delegates

我的一个 Controller 中有一个 UDPSocket,我知道我必须使用委托(delegate)。我必须添加哪些功能才能使其从另一个 Controller 访问我的 udpsocket?

使用像这样的东西会很棒

[controller1.udpsocket sendData:data toHost:host] 

但我知道它不好,该怎么办呢?

最佳答案

#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];
}

关于ios - 如何使用委托(delegate)从一个 Controller 向另一个 Controller 发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9872859/

相关文章:

ios - 在iOS设备中安装应用程序后如何一次显示警报

ios - 重用 Storyboard segue 中涉及的细节 UIViewController

iphone - 清除委托(delegate)

c# - 如何在 C# 中为委托(delegate)类型的参数提供默认值?

ios - iOS 中的基本表达语言

ios - 如何隐式设置 UITextView contentSize? ( swift 3/xCode8)

ios - 编辑另一个单元格时, Collection View 自动跳转到第一个单元格

ios - UI 图像/多 View Controller

ios - 如何同时呈现两个 View Controller ?

xcode - 使用调试器断点在 Xcode 8.1/Swift 3.0 中调用两次委托(delegate)方法