Objective-C中类和方法的定义以及协议的使用

2020-01-14 18:44:57于丽

    }
实现required方法
复制代码
-(void) StartObjectiveC
{
    NSLog(@"@required,StartObjectiveC");
}
-(void)StartPrograming
{
    NSLog(@"@required,StartPrograming");
     
}
附:在iOS ViewController中使用时

 

假设A要跳转到B页面时需要在B页面中实现A中的protocol方法

.h文件

复制代码
#import <UIKit/UIKit.h>
@class AViewController;
@protocol ADelegate <NSObject>
 
- (void) AMethod
 
@end
复制代码
@interface AViewController : UIViewController
{
     id<ADelegate> _delegate;
     
}
@property(nonatomic,assign)id<ADelegate> delegate;
@end
在页面跳转之前加上
复制代码
if (_delegate && [_delegate respondsToSelector:@selector(AMethod)]) {
          [_delegate AMethod];
     }
在B页面中声明代理<ADelegate>,实现AMethod方法,实例化A对象,并设置代理
则在B界面会调用AMethod方法,即完成了protocol的实现

注:相关教程知识阅读请移步到IOS开发频道。