我正在尝试使用一些自定义方法来扩展标准 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 继承自定义 View Controller 。
最佳答案
创建一个文件“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>
@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
关于ios - 创建类似于扩展 Swift 的 UIViewController 类别 (Obj-C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46217694/