ios-委托(Delegates)

委托(Delegates)示例

假设对象A调用B来执行一项操作,操作一旦完成,对象A就必须知道对象B已完成任务且对象A将执行其他必要操作。

在上面的示例中的关键概念有

  • A是B的委托对象

  • B引用一个A

  • A将实现B的委托方法

  • B通过委托方法通知

创建一个委托(Delegates)对象

  1. 创建一个单一视图的应用程序

  2. 然后选择文件 File -> New -> File…

file

  1. 然后选择Objective C单击下一步

  2. 将SampleProtocol的子类命名为NSObject,如下所示

file

  1. 然后选择创建

6.向SampleProtocol.h文件夹中添加一种协议,然后更新代码,如下所示:

#import <Foundation/Foundation.h>
// 协议定义
@protocol SampleProtocolDelegate <NSObject>
@required
- (void) processCompleted;
@end
// 协议定义结束
@interface SampleProtocol : NSObject

{
   // Delegate to respond back
   id <SampleProtocolDelegate> _delegate; 

}
@property (nonatomic,strong) id delegate;

-(void)startSampleProcess; // Instance method

@end

7.修改 SampleProtocol.m 文件代码,实现实例方法:

#import "SampleProtocol.h"

@implementation SampleProtocol

-(void)startSampleProcess{

    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self.delegate 
    selector:@selector(processCompleted) userInfo:nil repeats:NO];
}
@end
  1. 将标签从对象库拖到UIView,从而在ViewController.xib中添加UILabel,如下所示:

file

  1. 创建一个IBOutlet标签并命名为myLabel,然后按如下所示更新代码并在ViewController.h里显示SampleProtocolDelegate
#import <UIKit/UIKit.h>
#import "SampleProtocol.h"

@interface ViewController : UIViewController<SampleProtocolDelegate>
{
    IBOutlet UILabel *myLabel;
}
@end
  1. 完成授权方法,为SampleProtocol创建对象和调用startSampleProcess方法。如下所示,更新ViewController.m文件
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    SampleProtocol *sampleProtocol = [[SampleProtocol alloc]init];
    sampleProtocol.delegate = self;
    [myLabel setText:@"Processing..."];
    [sampleProtocol startSampleProcess];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Sample protocol delegate
-(void)processCompleted{    
    [myLabel setText:@"Process Completed"];
}

@end
  1. 将看到如下所示的输出结果,最初的标签也会继续运行,一旦授权方法被SampleProtocol对象所调用,标签运行程序的代码也会更新。

file

若文章对你有帮助,可以点赞或打赏支持我们。发布者:lyh会员,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/%e5%a7%94%e6%89%98%ef%bc%88delegates%ef%bc%89%e7%a4%ba%e4%be%8b/

(0)
lyhlyh会员认证作者
上一篇 2023年 4月 10日 下午9:22
下一篇 2023年 4月 10日 下午10:13

相关推荐

  • bzdiff

    文章目录bzdiff补充说明语法参数 bzdiff 直接比较两个.bz2压缩包中文件的不同 补充说明 bzdiff命令 用于直接比较两个“.bz2”压缩包中文件的不同,省去了解压缩…

    入门教程 2023年 12月 6日
  • iotop

    文章目录iotop补充说明安装语法选项实例 iotop 用来监视磁盘I/O使用状况的工具 补充说明 iotop命令 是一个用来监视磁盘I/O使用状况的top类工具。iotop具有与…

    入门教程 2023年 12月 19日
  • Java StringBuffer 和 StringBuilder 类

    当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。 和 String 类不同的是,StringBuffer 和 StringBuil…

    2023年 3月 4日
  • hping3

    文章目录hping3补充说明安装选项Hping3功能 防火墙测试端口扫描Idle扫描拒绝服务攻击文件传输木马功能 hping3 测试网络及主机的安全 补充说明 hping 是用于生…

    入门教程 2023年 12月 15日
  • fuser

    文章目录fuser补充说明语法选项参数实例 fuser 使用文件或文件结构识别进程 补充说明 fuser命令 用于报告进程使用的文件和网络套接字。fuser命令列出了本地进程的进程…

    入门教程 2023年 12月 14日
  • apt-get

    文章目录apt-get补充说明语法选项参数实例 apt-get Debian Linux发行版中的APT软件包管理工具 补充说明 apt-get命令 是Debian Linux发行…

    入门教程 2023年 12月 6日
  • chsh

    文章目录chsh补充说明语法选项参数实例 chsh 用来更换登录系统时使用的shell 补充说明 chsh命令 用来更换登录系统时使用的shell。若不指定任何参数与用户名称,则c…

    入门教程 2023年 12月 7日
  • cut

    文章目录cut补充说明语法选项参数实例指定字段的字符或者字节范围 cut 连接文件并打印到标准输出设备上 补充说明 cut 命令 用来显示行中的指定部分,删除文件中指定字段。cut…

    入门教程 2023年 12月 7日
  • HTML 链接

    HTML 使用超级链接与网络上的另一个文档相连。几乎可以在所有的网页中找到链接。点击链接可以从一张页面跳转到另一张页面。 文章目录HTML 超链接(链接)HTML 链接语法实例HT…

    2023年 4月 12日
  • vgremove

    文章目录vgremove补充说明语法选项参数实例 vgremove 用于用户删除LVM卷组 补充说明 vgremove命令 用于用户删除LVM卷组。当要删除的卷组上已经创建了逻辑卷…

    入门教程 2024年 3月 11日

发表回复

登录后才能评论
Translate »