我需要为我的所有UIViewControllers附加一个方法。该方法只是返回一个指向我的主应用程序委托类对象的指针。但是,xcode 4在声明输出参数类型为MyAppDelegate的头文件中引发错误“解析期望的类型”。如果我将其更改为其他类型,例如id,则错误消失。但是我使用点语法来访问主应用程序委托属性,如果我将该类型更改为ID,则xcode4无法识别我的主应用程序委托属性。我已经将类的定义文件包含到了我访问此方法的那些UIViewController类文件中。这里是我的类定义:访问链式方法时,类别是否允许使用点语法?
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "MyAppDelegate.h"
@interface UIViewController (MyCategory)
-(MyAppDelegate *) appDelegate; // xcode 4 complains about MyAppDelegate type, though it autocompletes it and show even in green color.
@end
这里是一个实现:
#import "MyCategory.h"
@implementation UIViewController (MyCategory)
-(MyAppDelegate *)appDelegate{
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
return delegate;
}
编辑:为什么我采取这一类的原因是,我需要有方便快捷的访问我的主要的应用程序从代码的任何地方委托(在我的情况下,从UIViewControler对象):
// handy shortcut :)
self.appDelegate.someMethod;
//not so handy shortcut :(
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
如何声明MyAppDelegate? –
@interface MyAppDelegate:NSObject。它只是实现UIApplicationDelegate协议。 –
Centurion
然后尝试'id delegate = ...'来代替。 –