【发布时间】:2011-12-04 18:35:37
【问题描述】:
我想将一个示例代码与我的 iPhone 应用程序(例如 SampleCode 项目)集成。在 firstViewController 中的示例代码中添加到 MainWindow.xib 并链接到在以下代码中创建的viewController。
@interface SampleCodeAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
firstViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet firstViewController *viewController;
和 viewController 用 initWithCoder 实例化,当 firstView 出现在按钮上时,可以通过调用 OpenCamera 方法打开相机,如下面的代码所示。
//in firstViewController.mm file
- (id)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
}
[self initLocal];
return self;
}
//to open camera in SampleCode application
- (IBAction)OpenCamera {
UIApplication *app = [UIApplication sharedApplication];
SampleCodeAppDelegate* delegate = [app delegate];
UIViewController* uiViewCont = [delegate viewController];
((CCamera*)m_pCamera)->Camera(uiViewCont);
}
在我的基于导航的应用程序 (MyApplication) 中,我想直接使用 viewController MyApplicationViewControllerA 之一调用 SampleCode 的 firstViewController,而不添加到 MyApplicationAppDelegate。
所以如果我在 MyApplicationViewControllerA viewController 中创建委托,我希望它应该作为 SampleCode 应用程序中的 appdelegate。 我无法打开相机,但在关闭相机后,我无法打开 MyApplication 的任何其他视图,但 MyApplicationViewControllerA 除外。 我正在尝试 pushViewController 和 modalViewController 无法呈现其他视图。
我对委托并不感到困惑。所以我想知道 AppDelegate(in SampleCodeAppDelegate : NSObject <UIApplicationDelegate>) 和其他 ViewContrller 中声明的委托有什么区别。
【问题讨论】:
-
[self initLocal];应该在您的“if (self = [super init...)”块内。如果超级初始化失败,你不想尝试设置你的对象;只需返回 self(这将是 nil)。
标签: iphone objective-c delegates uinavigationcontroller uiapplicationdelegate