創建一個包含文件 「的UIViewController + Alert.h」:
#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
然後,創建一個文件,其中包含 「的UIViewController + Alert.m」:
#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
享受!
把你的代碼放在這裏不是圖像的代碼 –
這是一個類,對吧?然後,只需在UIViewController子類對象上導入.h文件。 – Larme
將.h文件導入到我的自定義視圖控制器不起作用。擴展方法不可用。 – user267140