创建一个包含文件 “的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