0

我正在使用两个这样的协议:

@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;

有人可以帮我解决这个问题。
谢谢

4

2 回答 2

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;

    }
}
于 2013-09-27T10:53:56.483 回答
1

只需像这样声明您的 genericVC:

UIViewController<ModalClosedProtocol, syncmlClient> *genericVC;

这样您就可以让编译器知道您的 genericVC 视图控制器符合 ModalClosedProtocol 和 syncmlClient 协议,因此您要调用的方法就在那里。

于 2013-09-27T11:01:34.550 回答