모든 UIViewController에 메서드를 연결해야합니다. 메서드는 단지 내 주요 애플 리케이션 클래스 개체에 대한 포인터를 반환합니다. 그러나 xcode 4는 출력 매개 변수 유형 MyAppDelegate의 선언에서 헤더 파일에 "구문 분석 문제 유형이 예상됩니다"라는 오류를 발생시킵니다. 다른 유형 (예 : id)으로 변경하면 오류가 사라집니다. 하지만 주요 응용 프로그램 대리인 속성에 액세스 할 수있는 도트 구문을 사용하고 xcode4 형식을 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;
}
편집 : 여기 내 카테고리의 정의는 내가이 범주를 구현하고있어 이유는 내가 내 주요 응용 프로그램에 액세스하기위한 편리한 바로 가기를 가질 필요가있다
// handy shortcut :)
self.appDelegate.someMethod;
//not so handy shortcut :(
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
MyAppDelegate는 어떻게 선언됩니까? –
@interface MyAppDelegate : NSObject. 그것은 단지 UIApplicationDelegate 프로토콜을 구현 한 것입니다. –
Centurion
그런 다음'id delegate = ...'을 대신 사용해보십시오. –