gpt4 book ai didi

ios - 重构ViewController(重复逻辑)

转载 作者:行者123 更新时间:2023-11-29 11:48:51 27 4
gpt4 key购买 nike

在我的应用中,每个 ViewController 都有相同的逻辑。我想将它提取到一个类中。

这是我的案例。

每个 ViewController 都有各种发送 iMessage 的逻辑。 View Controller 的总数约为 50。

这是第一个 VC。

@interface FirstViewController : UIViewController <MFMessageComposeViewControllerDelegate>
....
- (void)sendMessage {
MFMessageComposeViewController *mfvc = [[MFMessageComposeViewController alloc] init];
.....
[self presentViewController:mfvc animated:YES completion:^{}];
}

// Delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:^{}];
}

这是第二个 VC。

@interface SecondViewController : UIViewController <MFMessageComposeViewControllerDelegate>
....
- (void)sendMessage {
MFMessageComposeViewController *mfvc = [[MFMessageComposeViewController alloc] init];
.....
[self presentViewController:mfvc animated:YES completion:^{}];
}

// Delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:^{}];
}

大约有 50 个这样的 View Controller 。

所以,我想按如下方式更改它。

@interface SendMessageHelper : XXX <MFMessageComposeViewControllerDelegate>
....
- (void)sendMessage:(NSString *messageBody) {
// All the code that sends the iMessage include "presentViewController"
}

// Delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
// Dismiss iMessage ViewController
}

///////////////////////////////////////////////////////////////////

@interface FirstViewController : UIViewController
....
- (void)sendMessage {
// Just call SendMessageHelper's sendMessage:
}

@interface SecondViewController : UIViewController
....
- (void)sendMessage {
// Just call SendMessageHelper's sendMessage:
}

....

@interface FiftiethViewController : UIViewController
....
- (void)sendMessage {
// Just call SendMessageHelper's sendMessage:
}

是否可以在单独的类中处理所有与 MFMessageComposeViewController 相关的逻辑和委托(delegate)?我该如何实现?

如果不行,请指教更好的方法。

最佳答案

您有多种选择。您可以创建一个 UIViewController 的子类来实现您的方法,并使所有 50 个 View Controller 成为该自定义子类的子类。

在这种情况下,在添加了这些方法的 UIViewController 上创建一个类别可能会更好:

标题:

@interface UIViewController (Messages);
// Delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *) controller
didFinishWithResult:(MessageComposeResult)result;
@end

.m 文件:

@implementation UIViewController (Messages) {
- (void)sendMessage {
MFMessageComposeViewController *mfvc =[[MFMessageComposeViewController alloc] init];
.....
[self presentViewController:mfvc animated:YES completion:^{}];
}

// Delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:^{}];
}
@end

然后,如果您#import 头文件,那么这些方法将可用。

关于ios - 重构ViewController(重复逻辑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42138501/

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