iOS 中代理方法使用步骤

简单易懂,实现代理方法

  1. viem.h 中声明
@protocol YQCourseHomeMallViewDelegate

@optional

- (void)mallView:(YQCourseHomeMallView *)mallView didClickItemAtIndex:(NSInteger)index;

@end
  1. viem.m 中触发
if (self.delegate && [self.delegate respondsToSelector:@selector(mallView:didClickItemAtIndex:)]) {
        [self.delegate mallView:self didClickItemAtIndex:btn.tag - 1000];
    }
  1. 调用View的地方.delegate = self;
- (YQCourseHomeMallView *)courseMallView {
    if (!_courseMallView) {
        _courseMallView = [[YQCourseHomeMallView alloc] initWithFrame:CGRectZero];
        _courseMallView.delegate = self;
    }
    return _courseMallView;
}
  1. 重新实现代理方法。
#pragma mark -
#pragma mark YQCourseHomeMallViewDelegate
#pragma mark -

- (void)mallView:(YQCourseHomeMallView *)mallView didClickItemAtIndex:(NSInteger)index {
  // do something
}

你可能感兴趣的:(iOS 中代理方法使用步骤)