我正在使用这样的两个协议:
@protocol ModalClosedProtocol <NSObject>
-(void) modalClosedGlobalProtocolMethod;
@end
和syncmlClient协议。
有三个类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;
可以帮助我解决这个问题 感谢
答案 0 :(得分:1)
[self checkDeviceAndHandleModelSizeForFlip:self.flipVc];//Pass contactVc, eventVc, imageSettingVc like that,
-(void) checkDeviceAndHandleModelSizeForFlip:(ViewController *)viewController
{
UIViewController *genericVC;
if (viewController!=nil)
{
genericVC = viewController;
viewController.modalClosedProtocolDelegate = self;
viewController.syncmlClient = self.syncmlClient;
viewController.mainViewController = self.mainViewController;
}
}
答案 1 :(得分:1)
只需声明你的genericVC:
UIViewController<ModalClosedProtocol, syncmlClient> *genericVC;
通过这种方式,您可以让编译器知道您的genericVC View Controller符合协议ModalClosedProtocol和syncmlClient,因此您要调用的方法就在那里。