objective-c – IOS应用程序 – 在单独的文件中定义协议

前端之家收集整理的这篇文章主要介绍了objective-c – IOS应用程序 – 在单独的文件中定义协议前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个单独的文件(myProtocol.h)中定义了一个协议.这是它的代码
  1. #import <Foundation/Foundation.h>
  2.  
  3. @protocol myProtocol <NSObject>
  4. -(void) loadDataComplete;
  5. @end

现在我想调用这个方法,所以我做了以下代码

firstViewController.h:

  1. #import "myProtocol.h"
  2.  
  3. @interface firstViewController : UIViewController{
  4. id <myProtocol> delegate;
  5. }
  6. @property (retain) id delegate;
  7. -(void) mymethod;

firstViewController.m

  1. @implementation firstViewController
  2. @synthesize delegate;
  3.  
  4. - (void)viewDidLoad {
  5. [self mymethod];
  6. }
  7.  
  8. -(void) mymethod {
  9. //some code here...
  10. [delegate loadDataComplete];
  11. }

我有另一个文件,其中也使用协议:

secondViewController.h:

  1. #import "myProtocol.h"
  2. @interface secondViewController : UIViewController<myProtocol>{
  3. }

secondViewController.m:

  1. -(void) loadDataComplete{
  2. NSLog(@"loadDataComplete called");
  3. }

但我的secondViewController没有调用协议美联储.为什么会这样?任何建议将不胜感激.

解决方法

首先,正如@Abizern建议的那样,尝试重新格式化代码.使用大写字母表示课程.这里说这个解答你的答案.

这是协议.我将它命名为FirstViewControllerDelegate,因为实现该对象的类是FirstViewController的委托.

  1. #import <Foundation/Foundation.h>
  2.  
  3. @protocol MyProtocol <NSObject>
  4.  
  5. - (void)doSomething;
  6.  
  7. @end

这是SecondViewController.

  1. #import <UIKit/UIKit.h>
  2. #import "MyProtocol.h"
  3.  
  4. @interface SecondViewController : UIViewController <MyProtocol>
  5.  
  6. @end
  7.  
  8. @implementation SecondViewController
  9.  
  10. // other code here...
  11.  
  12. - (void)doSomething
  13. {
  14. NSLog(@"Hello FirstViewController");
  15. }
  16.  
  17. @end

这是FirstViewController.

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface FirstViewController : UIViewController
  4.  
  5. // it coud be better to declare these properties within a class extension but for the sake of simplicity you could leave here
  6. // the important thing is to not declare the delegate prop with a strong/retain property but with a weak/assign one,otherwise you can create cycle
  7. @property (nonatomic,strong) SecondViewController* childController;
  8. @property (nonatomic,weak) id<MyProtocol> delegate;
  9.  
  10. @end
  11.  
  12. @implementation FirstViewController
  13.  
  14. // other code here...
  15.  
  16. - (void)viewDidLoad
  17. {
  18. [super viewDidLoad];
  19.  
  20. self.childController = [[SecondViewController alloc] init];
  21. self.delegate = self.childController; // here the central point
  22.  
  23. // be sure your delegate (SecondViewController) responds to doSomething method
  24. if(![self.delegate respondsToSelector:@selector(doSomething)]) {
  25.  
  26. NSLog(@"delegate cannot respond");
  27. } else {
  28.  
  29. NSLog(@"delegate can respond");
  30. [self.delegate doSomething];
  31. }
  32. }
  33.  
  34. @end

为了完整起见,请务必了解委托模式的含义. Apple doc是你的朋友.您可以查看the-basics-of-protocols-and-delegates以获得参数的基本介绍.此外,SO搜索允许您找到关于该主题的大量答案.

希望有所帮助.

猜你在找的C&C++相关文章