gpt4 book ai didi

ios - 创建类似于 Extension Swift 的 UIViewController Category (Obj-C)

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:22:37 24 4
gpt4 key购买 nike

我正在尝试使用一些自定义方法扩展标准 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 - 创建类似于 Extension Swift 的 UIViewController Category (Obj-C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46217694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com