重构ViewController(重复逻辑)
Refactoring ViewController(repetitive logic)
在我的应用中,每个 ViewController 都有相同的逻辑。
我想把它提取成一个 class.
这是我的案例。
每个 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 个这样的视图控制器。
所以,我想按如下方式更改它。
@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:
}
是否可以在单独的 classes 中处理所有 MFMessageComposeViewController 相关逻辑和委托?
我该如何实施?
如果不行,请指教更好的方法
您有多种选择。您可以创建一个 UIViewController 的子类来实现您的方法,并使所有 50 个视图控制器成为该自定义子类的子类。
在这种情况下,在 UIViewController 上创建一个类别并添加这些方法可能会更好:
header:
@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
header 文件,这些方法将可用。
在我的应用中,每个 ViewController 都有相同的逻辑。 我想把它提取成一个 class.
这是我的案例。
每个 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 个这样的视图控制器。
所以,我想按如下方式更改它。
@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:
}
是否可以在单独的 classes 中处理所有 MFMessageComposeViewController 相关逻辑和委托? 我该如何实施?
如果不行,请指教更好的方法
您有多种选择。您可以创建一个 UIViewController 的子类来实现您的方法,并使所有 50 个视图控制器成为该自定义子类的子类。
在这种情况下,在 UIViewController 上创建一个类别并添加这些方法可能会更好:
header:
@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
header 文件,这些方法将可用。