1

표준 사용자 정의 방법으로 표준 UIViewController을 확장하려고합니다.UIViewController extension

#import <UIKit/UIKit.h> 

@interface UIViewController (UIViewControllerExtension) 
- (void) showNoHandlerAlertWithTitle:(NSString *)title andMessage:  (NSString*)message; 
- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString*)message buttonTitles:(NSArray<NSString *>*)titles andHandler:(void (^)(UIAlertAction * action))handler; 
@end 

확장 된 UIViewController은 어떻게 사용할 수 있습니까? 내 사용자 정의보기 컨트롤러를 상속 한 UIViewController에서 상속해야합니다.

#import "UIViewController+Alert.h" 
@implementation UIViewController (AlertExtension) 
- (void) showNoHandlerAlertWithTitle:(NSString *)title andMessage:  (NSString*)message { 
    // Insert code here 
} 

- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString*)message buttonTitles:(NSArray<NSString *>*)titles andHandler:(void (^)(UIAlertAction * action))handler { 
    // Insert code here 
} 
@end 

에서 당신의 "SampleViewController 말 : 파일 포함"의 UIViewController + Alert.m "를 만든 다음

#import <UIKit/UIKit.h> 
@interface UIViewController (AlertExtension) 
- (void) showNoHandlerAlertWithTitle:(NSString *)title andMessage:  (NSString*)message; 
- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString*)message buttonTitles:(NSArray<NSString *>*)titles andHandler:(void (^)(UIAlertAction * action))handler; 
@end 

을 :

+2

여기에 코드를 입력하십시오. 코드 –

+0

의 이미지가 아닙니다. 맞습니까? 그런 다음 .h 파일을 UIViewController 하위 클래 싱 된 객체로 가져옵니다. – Larme

+0

.h 파일을 사용자 지정보기 컨트롤러로 가져 오는 기능이 작동하지 않습니다. 확장 메서드를 사용할 수 없습니다. – user267140

답변

1

파일"의 UIViewController + Alert.h "를 포함 만들기 .H "

에서 다음
#import <UIKit/UIKit.h> 
#import "UIViewController+Alert.h" 

@interface SampleViewController : UIViewController 
@end 

"SampleViewController.m "

#import "SampleViewController.h" 
@implementation SampleViewController 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self showNoHandlerAlertWithTitle:@"Hello" andMessage:@"World"]; 
} 
@end 

즐기십시오!

+1

구현시 UIViewController + Alert.h를 가져 오기만하면 충분합니다. 그렇지 않으면 헤더가 오염됩니다. 또한'@import UIKit;은'#import '보다 우선되어야합니다. – Sulthan

+0

Xcode의 버전에 대한 추가 컨텍스트가없고 @import UIKit을 사용하여 빌드 설정에서 "Enable Modules"이 YES로 설정되었는지 여부. 실제로 코드가 컴파일되지 않게 할 수 있습니다. 나는주의의 측면에서 상속 받고 질문에 사용 된 것과 유사한 구문을 다시 사용하는 것을 선호했다. 즉, 코드의 다른 부분이 뷰 컨트롤러에서 이러한 함수를 사용할 수 없으면 UIViewController + Alert.h를 구현에만 가져 오는 것이 옳습니다. – ekscrypto