使用一个具有多个类的 Xib

Posted

技术标签:

【中文标题】使用一个具有多个类的 Xib【英文标题】:Working with One Xib with Multiple Classes 【发布时间】:2015-08-24 06:59:32 【问题描述】:

我已经在这里问了一个问题Handling Multiple Xib or Fixing collapse and expand button on multiple Xib file 然后我想停止使用多个 xib 并且只使用具有不同类的主 xib 但有可能吗? 有人可以指导我做我可能做的事情吗?

请帮我解决它

使用 UIbutton 的代码

- (IBAction)btnPupl:(UIButton *)sender 
CGRect rect;
 sender.selected = !sender.selected;


if(sender.selected )


[sender setImage:[UIImage imageNamed:@"plus_1.png"]forState:UIControlStateNormal];
    pView.hidden = true;
    eomView.hidden = true ;
    eomLable.hidden = true;

        rect = pupileomMainView.frame;
        rect.size.height = rect.size.height - eomView.frame.size.height ;
        pupileomMainView.frame = rect;



    rect = pSepLine1.frame ;
    rect.origin.y = pupileomMainView.frame.size.height + pupileomMainView.frame.origin.y  ;
    pSepLine1.frame = rect ;

    rect = pSepLine2.frame ;
    rect.origin.y = pSepLine1.frame.origin.y +6 ;
    pSepLine2.frame = rect;

    rect = ExternalMainView.frame;
    rect.origin.y = pSepLine2.frame.origin.y + 1;
    ExternalMainView.frame = rect;

    rect = eSepLine1.frame;
    rect.origin.y = ExternalMainView.frame.origin.y + ExternalMainView.frame.size.height  ;
    eSepLine1.frame = rect;

    rect = eSepLine2.frame;
    rect.origin.y = eSepLine1.frame.origin.y + 5 ;
    eSepLine2.frame = rect ;

    rect = laMainView.frame;
    rect.origin.y = eSepLine2.frame.origin.y + 1;
    laMainView.frame = rect;

    rect = laSepLine1.frame;
    rect.origin.y = laMainView.frame.origin.y +laMainView.frame.size.height  ;
    laSepLine1.frame = rect ;

    rect = laSepLine2.frame ;
    rect.origin.y = laSepLine1.frame.origin.y + 5 ;
    laSepLine2.frame = rect ;

    rect = iopMainView.frame;
    rect.origin.y = laSepLine2.frame.origin.y +1;
    iopMainView.frame = rect;

    rect = iopSepLine1.frame;
    rect.origin.y = iopMainView.frame.origin.y + iopMainView.frame.size.height ;
    iopSepLine1.frame = rect ;

    rect = iopSepLine2.frame;
    rect.origin.y = iopSepLine1.frame.origin.y + 5;
    iopSepLine2.frame = rect;

    rect = sleMainView.frame;
    rect.origin.y = iopSepLine2.frame.origin.y + 1 ;
    sleMainView.frame = rect;

    rect = sleSepLine.frame;
    rect.origin.y = sleMainView.frame.size.height + sleMainView.frame.origin.y ;
    sleSepLine.frame = rect;

    rect = sleSepLine2.frame;
    rect.origin.y = sleSepLine.frame.origin.y +5;
    sleSepLine2.frame = rect;

    rect = fundusMainView.frame;
    rect.origin.y = sleSepLine2.frame.origin.y + 1;
    fundusMainView.frame = rect;

    rect= fundusSepLine1.frame;
    rect.origin.y = fundusMainView.frame.origin.y + fundusMainView.frame.size.height ;
    fundusSepLine1.frame = rect ;

    rect= fundusSepLine2.frame;
    rect.origin.y = fundusSepLine1.frame.origin.y+5 ;
    fundusSepLine2.frame = rect ;

    rect = self.examView.frame;
    rect.size.height = rect.size.height - eomView.frame.size.height;
    self.examView.frame = rect;


else 

    pView.hidden = false;
    eomView.hidden = false;
    eomLable.hidden = false;

    [sender setImage:[UIImage imageNamed:@"minus_round_d1.png"]forState:UIControlStateNormal];




    rect = pupileomMainView.frame;
    rect.size.height =  rect.size.height + eomView.frame.size.height  ;
    pupileomMainView.frame = rect;

    rect = pSepLine1.frame ;
    rect.origin.y = pupileomMainView.frame.size.height + pupileomMainView.frame.origin.y +1 ;
    pSepLine1.frame = rect ;

    rect = pSepLine2.frame ;
    rect.origin.y = pSepLine1.frame.origin.y +6 ;
    pSepLine2.frame = rect;

    rect = ExternalMainView.frame;
    rect.origin.y = pSepLine2.frame.origin.y + 1;
    ExternalMainView.frame = rect;

    rect = eSepLine1.frame;

【问题讨论】:

这个问题需要澄清一下:你想让不同的Objective-C类都使用一个XIB文件吗?还是从单个 Objective-C 类调用多个 XIB 文件? 谢谢你的回复其实看看我的老问题 如果通过其他方式解决了空白问题,那么我不需要其他方法 @iDeepak 我不认为这是处理多个 XIB 的问题,您的实现存在问题。你能把你的代码放在这里吗? 完整代码???它包含更多无法放在这里的编解码器 【参考方案1】:

是的,可以使用polymorphism。这是可以向多个类发送相同消息的想法,(并且可能以不同的方式表现)。您可以创建一个类,如:BaseViewController,它在@interface 中具有btnPupl:(id)sender 的定义。这样,任何继承自 BaseViewController 的视图控制器都将继承 btnPupl:(id)sender

另一个选择(我会选择第一个)是子类UIButton 并在该子类中实现您想要的行为。

【讨论】:

以上是关于使用一个具有多个类的 Xib的主要内容,如果未能解决你的问题,请参考以下文章

使用具有相同 UIViewController 类的不同 xib 项

具有多个 XIB 的 UIPageViewController

具有相同 XIB 单元格和按钮的多个集合视图

具有多个 xib 的 PageControl?

跨多个 NSViewController 和 XIB 拆分 NSTabView

XIB 文件中对象的多个实例