使用delegate的例子

其实这个例子本身是从网上找到的,链接网址为:点我啊

虽然我很讨厌这样什么也不干就贴个别人写的文章,但是这篇文章实在是我当前需要的,并且,我现在之所以打上这些字就是为了让这片文章看上去除了别人的痕迹之外还有我自己的。
此外我不得不补充一下,当在创建IBoutlet时别忘了 把view controller中的Label使用outlet和viewcontroller中的 IBoutlet关联起来,也就是先按下Ctrl,然后用鼠标左键点在Label上,别撒手,一直连接到IBOutlet UILabel *mylabel上,这样才算把代码中的mylabel和实际view中的label关联起来,否则最后运行时view 中的Label还是现实Label,并没有我期望的process... 和process completed。哦对了,还得记得把Label的尺寸拉大一些,否则会装不下process...和process completed,最后看起来的效果是 “process com” 后面剩余的字符串就不显示了。


Example for Delegate

Let's assume an object A calls an object B to perform an action. Once the action is complete, object A should know that B has completed the task and take necessary action. This is achieved with the help of delegates.

The key concepts in the above example are −

  • A is a delegate object of B.

  • B will have a reference of A.

  • A will implement the delegate methods of B.

  • B will notify A through the delegate methods.

Steps in Creating a Delegate

step 1. First, create a single view application.

step 2. Then select File -> New -> File...

iOS Tutorial

step 3. Then select Objective C Class and click Next.

step 4. Give a name to the class, say, SampleProtocol with subclass as NSObject as shown below.

iOS Tutorial

step 5. Then select create.

step 6. Add a protocol to the SampleProtocol.h file and the updated code is as follows −

#import <Foundation/Foundation.h>
// Protocol definition starts here 
@protocol SampleProtocolDelegate <NSObject>
@required
- (void) processCompleted;
@end
// Protocol Definition ends here
@interface SampleProtocol : NSObject

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

}
@property (nonatomic,strong) id delegate;

-(void)startSampleProcess; // Instance method

@end

step 7. Implement the instance method by updating the SampleProtocol.m file as shown below.


#import "SampleProtocol.h"

@implementation SampleProtocol

-(void)startSampleProcess{
    
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self.delegate 
	selector:@selector(processCompleted) userInfo:nil repeats:NO];
}
@end

step 8. Add a UILabel in the ViewController.xib by dragging the label from the object library to UIView as shown below.

iOS Tutorial

step 9. Create an IBOutlet for the label and name it as myLabel and update the code as follows to adopt SampleProtocolDelegate in ViewController.h.


#import <UIKit/UIKit.h>
#import "SampleProtocol.h"

@interface ViewController : UIViewController<SampleProtocolDelegate>
{
    IBOutlet UILabel *myLabel;
}
@end

step 10. Implement the delegate method, create object for SampleProtocol and call the startSampleProcess method. The Updated ViewController.m file is as follows −

#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

step 11. We will see an output as follows. Initially the label displays "processing...", which gets updated once the delegate method is called by the SampleProtocol object.

iOS Tutorial

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值