我正在使用两个这样的协议:
@protocol ModalClosedProtocol <NSObject>
-(void) modalClosedGlobalProtocolMethod;
@end
和 syncmlClient 协议。
有 3 个类ContactsViewController, EventViewController, ImageSettingViewController。这些类使用ModalclosedProtocol和 syncmlClient 协议。所有这三个类都期望SettingViewController类实现ModalClosedProtocol和 syncmlClient 协议。这只是当前实现的简短概述。
在我SettingViewController处理表委托方法中。didSelectRow对于代码分解,我创建了一个单独的方法,该方法从didSelectRow.. 调用,如下所示:
-(void) checkDeviceAndHandleModelSizeForFlip:(FlipsideViewController *)flipVc orContact:(ContactsViewController *)contactVc orEvent:(EventViewController *)eventVc orImage:(ImageSettingViewController *)imageSettingVc
{
UIViewController *genericVC;
if (flipVc!=nil)
{
genericVC = flipVc;
flipVc.modalClosedProtocolDelegate = self;
flipVc.syncmlClient = self.syncmlClient;
}
else if (contactVc!=nil)
{
genericVC = contactVc;
contactVc.modalClosedProtocolDelegate = self;
contactVc.syncmlClient = self.syncmlClient;
contactVc.mainViewController = self.mainViewController;
}
else if (eventVc!=nil)
{
genericVC = eventVc;
eventVc.modalClosedProtocolDelegate = self;
eventVc.syncmlClient = self.syncmlClient;
eventVc.mainViewController = self.mainViewController;
}
else if (imageSettingVc!=nil)
{
genericVC = imageSettingVc;
imageSettingVc.modalClosedProtocolDelegate = self;
imageSettingVc.syncmlClient = self.syncmlClient;
imageSettingVc.mainViewController = self.mainViewController;
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
genericVC.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:genericVC animated:YES completion:nil];
genericVC.view.superview.frame = CGRectInset(genericVC.view.superview.frame, 100, 50);
}
else
{
genericVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:genericVC animated:YES completion:nil]; //alok
}
}
基本上,如果您看到子句中的代码有很多冗余。if/else从代码管理的角度来看,如果我有一个UIViewController对象会更好。
genericVC = contactVc;//vc object passed from didSelectrow it may be contact/event/image
genericVC.modalClosedProtocolDelegate = self;
genericVC.syncmlClient = self.syncmlClient;
genericVC.mainViewController = self.mainViewController;
有人可以帮我解决这个问题。
谢谢