我正在尝试使用一些自定义方法扩展标准UIViewController
。
#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
继承我的自定义视图控制器。
创建一个文件'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 quot;其中包含:
#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&quot"中:
#import <UIKit/UIKit.h>
@interface SampleViewController : UIViewController
@end
然后在" sampleviewController.M"中:
#import "SampleViewController.h"
#import "UIViewController+Alert.h"
@implementation SampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self showNoHandlerAlertWithTitle:@"Hello" andMessage:@"World"];
}
@end