OStack程序员社区-中国程序员成长平台

标题: ios - 创建类似于 Extension Swift 的 UIViewController 类别(Obj-C) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:35
标题: ios - 创建类似于 Extension Swift 的 UIViewController 类别(Obj-C)

我正在尝试使用一些自定义方法扩展标准 UIViewController

#import <UIKit/UIKit.h>

@interface UIViewController (UIViewControllerExtension)
- (void) showNoHandlerAlertWithTitleNSString *)title andMessage:     (NSString*)message;
- (void) showAlertWithTitleNSString *)title andMessageNSString*)message  buttonTitlesNSArray<NSString *>*)titles andHandlervoid (^)(UIAlertAction * action))handler;
@end

我现在如何使用扩展的 UIViewController?我需要从扩展的 UIViewController 继承我的自定义 View Controller 。



Best Answer-推荐答案


创建一个文件“UIViewController+Alert.h”,其中包含:

#import <UIKit/UIKit.h>
@interface UIViewController (AlertExtension)
- (void) showNoHandlerAlertWithTitleNSString *)title andMessage:     (NSString*)message;
- (void) showAlertWithTitleNSString *)title andMessageNSString*)message  buttonTitlesNSArray<NSString *>*)titles andHandlervoid (^)(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 - 创建类似于 Extension Swift 的 UIViewController 类别(Obj-C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46217694/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4