代码改变世界

iOS中delegate的用法

2013-07-01 18:30  王妞  阅读(1318)  评论(1编辑  收藏  举报

主要涉及到两个文件。

第一个文件,我称之为定义delegate的文件,要做的事:

1. 在BorderDetectionViewController.h中做两件事

(1)

@protocol BorderDetectionViewControllerDelegate <NSObject>
- (void)dismissBorderDetectionView;
@end

(2)

 

@property (nonatomic, assign) id<BorderDetectionViewControllerDelegate> delegate;
2.在BorderDetectionViewController.m中做两件事
(1)
@synthesize delegate;
(2)
[self.delegate dismissBorderDetectionView];
 
在第二个文件中,我称之为应用文件,要做的事:
1. 在MainTabViewController.h中做两件事:
(1)
#import "BorderDetectionViewController.h"
(2)
在interface后面加上<BorderDetectionViewControllerDelegate>
2. 在MainTabViewController.m中做两件事:
(1)在要present borderView时,设置borderViewController.delegate = self;
(2)实现该delegate函数
- (void)dismissBorderDetectionView
{
   [self dismissModalViewControllerAnimated:YES];
}