【问题标题】:Delegate Definition in iPhone SDK [closed]iPhone SDK中的委托定义[关闭]
【发布时间】:2011-08-24 07:46:20
【问题描述】:

谁能用实时场景解释 iphone sdk 中的 DELEGATE 定义。

对不起,我的问题很糟糕。

提前致谢。

【问题讨论】:

标签: iphone objective-c oop ios4


【解决方案1】:

委托是一个对象,它将在未来的某个时间响应预先选择的选择器(函数调用)。

假设我正在使用 FancyAsynchronousURLGetter 类的对象异步加载 URL(在后台)。由于它在后台运行,我希望能够在加载 url 时去做其他事情,然后在它准备好时得到通知。通过在 FancyAsynchronousURLGetter 上使用委托并编写适当的代码,我可以指定一个具有特定选择器的对象,该选择器将在 FancyAsynchronousURLGetter 完成时调用。像这样的:

- (void)loadView
{
    ...
    FancyAsynchronousURLGetter* getter = [[FancyAsynchronousURLGetter alloc] initWithURL:url];
    [getter setDelegate:self]; 
    /* 
    getter will call either 
        - (void)fancyAsynchronousURLGetterLoadSucceeded:(FancyAsynchronousURLGetter*)g
    or 
        - (void)fancyAsynchronousURLGetterLoadFailed:(FancyAsynchronousURLGetter*)g 
    on its delegate, depending on whether load succeeded or failed 
    */
    [getter start];
    ...
}

- (void)fancyAsynchronousURLGetterLoadSucceeded:(FancyAsynchronousURLGetter*)g
{
    NSLog(@"Load succeeded.");
}


- (void)fancyAsynchronousURLGetterLoadFailed:(FancyAsynchronousURLGetter*)g
{
    NSLog(@"Load failed.");
}

在 FancyAsynchronousURLGetter 本身中:

- (void)start
{
    [self performSelectorInBackground:@selector(fetchURL) withObject:nil];
}

- (void))fetchURL
{
    Fetch the URL synchronously
    if ( success )
        [delegate fancyAsynchronousURLGetterLoadSucceeded:self]; // note: probably want to call this on the main thread
    else
        [delegate fancyAsynchronousURLGetterLoadFailed:self];
}

【讨论】:

  • 我会用(方法调用)替换(函数调用)。方法调用与直接调用函数不同。而且您使用的方法名称是不可能的。使其成为一种委托方法,称为fancyAsynchronousURLGetter:(id)sender succeeded:(BOOL)success
  • 感谢 - 已更正(3 年后!哈哈)
【解决方案2】:

这里有一个示例代码可以理解。

协议定义

    #import <Foundation/Foundation.h>

    @protocol ProcessDataDelegate <NSObject>
    @required
    - (void) processSuccessful: (BOOL)success;
    @end

    @interface ClassWithProtocol : NSObject 
    {
        id <ProcessDataDelegate> delegate;
    }

    @property (retain) id delegate;

    -(void)startSomeProcess;

    @end

协议实现

    #import "ClassWithProtocol.h"

    @implementation ClassWithProtocol 
    @synthesize delegate;

    - (void)processComplete
    {
        [[self delegate] processSuccessful:YES];
    }

    -(void)startSomeProcess
    {
        [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector: @selector(processComplete) userInfo:nil repeats:YES];
    }

@结束

要获得完整的想法,visit here...

【讨论】:

    【解决方案3】:

    委托不取决于您使用的语言或平台,它是一种设计模式。我建议您阅读Delegation pattern 以进行基本了解。之后我认为您可以进一步了解。

    【讨论】:

      【解决方案4】:

      Short:Delegation 是两个对象之间的一对一关系,允许一个对象调用另一个对象的方法,它允许被调用者自定义实现该方法。

      委托允许您编写自定义实现方法的自定义实现。

      例如UITableView's Delegatedatasource(在技术上与委托相同)允许 UITableView 让其他类执行重要任务,例如创建单元格以显示在表格视图中并响应事件(例如点击表视图)。

      要首先使用委托,您需要定义一个协议:

      @protocol protocolNameHere 
      
      - (void) sampleMethodHere;
      
      @optional
      
      - (void) implementingThisMethodIsOptional;
      
      @end
      

      那么你必须在你想被委托的类的头文件前面的“”中添加协议的名称。该类现在符合该协议。您需要在此类中实现该方法。

      如果有多个代表,则用逗号分隔它们,例如

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        • 2011-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-29
        • 1970-01-01
        相关资源
        最近更新 更多