问题
I have already asked a question here Handling Multiple Xib or Fixing collapse and expand button on multiple Xib file then I thought to stop using multiple xib and only main xib with different classes but is it possible ?? Can someone guide me in way which is possible for me to do ?
Please help me to fix it
Code for UIbutton im using
- (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;
回答1:
Yes it is possible using polymorphism. This is the idea that multiple classes can be sent the same message, (and potentially behave in different ways). You can create a class like: BaseViewController
that would have the definition for btnPupl:(id)sender
in the @interface
. This way any view controller that inherits from BaseViewController
will inherit btnPupl:(id)sender
.
Another option (I'd go with the first one) is to subclass UIButton
and implement the behaviour you want in that subclass.
来源:https://stackoverflow.com/questions/32176153/working-with-one-xib-with-multiple-classes