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から継承する必要があります。

+2

ここにコードを入れてください –

+0

これはカテゴリの権利ですか?次に、UIViewControllerのサブクラス化されたオブジェクトに.hファイルをインポートするだけです。 – Larme

+0

.hファイルをカスタムビューコントローラにインポートする機能しません。拡張メソッドは利用できません。 – user267140

答えて

1

含む "のUIViewController + Alert.hを" ファイルを作成します "の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 

が含まれていること:

#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を言います.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