我将这个名为engine.xcodeproj的项目嵌入到另一个项目myApp中。
此引擎必须从MainViewController.h获取一个值,该值是应用程序类的标头,超出了engine.xcodeproj的范围。
如何让所有应用程序主路径对嵌入式项目可见!
我正在使用Xcode 5并为iOS 6进行编译。
我之前已经回答了这个问题,但这些问题的答案并没有解决这个问题......
见图:
感谢。
答案 0 :(得分:4)
嗯,这就是所谓的意大利面条代码。
最好在Engine项目中定义一个协议,视图控制器可以实现该协议,然后将id<Protocol>
传递给引擎。这在两个项目之间创建了一个抽象,同时在它们之间定义了一个强大的语言(API)。您提到您希望在多个应用程序中使用Engine项目 - 这是您最好的解决方案。
在发动机项目中:
@protocol MyAPIProtocol
@required
//Define here the actions you want to perform/get from the data source.
- (CGFloat)floatValue;
- (UITextView*)textView;
- (void)displayAlertWithMessage:(NSString*)message;
@end
现在,您的Rocket类应该具有如下定义的属性:
@property (nonatomic, weak) id<MyAPIProtocol> dataSource; //Whatever name you need, of course
现在在您使用此引擎项目的应用中:
#import "MyAPIProtocol.h"
@interface MainViewController () <MyAPIProtocol>
@end
@implementation MainViewController
...
//Implement the protocol
- (CGFloat)floatValue
{
return 123.0f;
}
- (UITextView*)textView
{
return self.textView;
}
- (void)displayAlertWithMessage:(NSString*)message
{
//...
}
@end
结果是Engine项目是自包含的,不需要知道MainViewController
的实现。它只知道它具有dataSource
属性,可以满足其所有需求。
现在,当你在MainViewController中准备好你的Engine对象时,你应该通过以下方式设置它的数据源:
self.engine.dataSource = self;