Iphone 试图将代码抽象到库中,但我的代码不是';不工作,我不知道为什么
我想做一个库,我可以重复使用,以加快我的发展,添加到新的项目,如果我愿意。基本上我想建立一个抽象层。有人能告诉我怎么做,为什么这部分代码:Iphone 试图将代码抽象到库中,但我的代码不是';不工作,我不知道为什么,iphone,objective-c,ios,Iphone,Objective C,Ios,我想做一个库,我可以重复使用,以加快我的发展,添加到新的项目,如果我愿意。基本上我想建立一个抽象层。有人能告诉我怎么做,为什么这部分代码: if ([self.delegate respondsToSelector:@selector(enableCamera)]) { BOOL enabled; enabled = [self.delegate enableCamera]; if (enabled == YES) {
if ([self.delegate respondsToSelector:@selector(enableCamera)]) {
BOOL enabled;
enabled = [self.delegate enableCamera];
if (enabled == YES) {
[self enableCameraMethod];
}
没有接到电话吗
下面是我的代码:
图书馆h:
@protocol usesCamera <NSObject>
@optional
-(BOOL)enableCamera;
@end
@interface Library : NSObject
@property (nonatomic, weak) id <usesCamera> delegate;
-(void)enableCameraMethod;
@end
UIViewController.h
#import <UIKit/UIKit.h>
#import "Library.h"
@interface ViewController : UIViewController <usesCamera>
@end
是否在ViewController类中为myLibrary实例设置了委托。 你必须这样做: 由于init是在设置委托之前调用的,因此它可能不起作用,而不是在init函数中定义逻辑,因此创建另一个方法并在设置委托后调用此方法
#import <UIKit/UIKit.h>
#import "Library.h"
@interface ViewController : UIViewController <usesCamera>
@end
#import "ViewController.h"
#import "Library.h"
@interface ViewController ()
@property (nonatomic, strong) UIViewController *myVC;
@end
@implementation ViewController
-(BOOL)enableCamera {
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
Library *myLibrary = [[Library alloc] init];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Library *myLibrary = [[Library alloc] init];
myLibrary.delegate = self;