[英]Creating UIViewController Category (Obj-C) similar to Extension Swift
我正在尝试使用一些自定义方法扩展标准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”,其中包含:
#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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.