Rob W
Rob W

Reputation: 611

How to override a small part of a method?

I have these methods in a class at the moment which seem to work fine. I would like to create a subclass which inherits these methods. The problem I have is that in the third method (shiftViewUpForKeyboard) I want the if statement to be a different UITextField (mirror being the current example). I've read that to override a method in the subclass, you have to basically copy it exactly with the new coding, but if I want to just change that small section what is the best way to do it? Thank you in advance.

- (void) viewWillAppear:(BOOL)animated {

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(shiftViewUpForKeyboard:)
                                             name: UIKeyboardWillShowNotification
                                           object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(shiftViewDownAfterKeyboard)
                                             name: UIKeyboardWillHideNotification
                                           object: nil];


}

- (void) viewDidDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver: self
                                                name: UIKeyboardWillShowNotification
                                              object: nil];
[[NSNotificationCenter defaultCenter] removeObserver: self
                                                name: UIKeyboardWillHideNotification
                                              object: nil];


}

- (void) shiftViewUpForKeyboard: (NSNotification*) theNotification;
{
if(mirror.isEditing == YES)
{

CGRect keyboardFrame;
NSDictionary* userInfo = theNotification.userInfo;
keyboardSlideDuration = [[userInfo objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];
keyboardFrame = [[userInfo objectForKey: UIKeyboardFrameBeginUserInfoKey] CGRectValue];

UIInterfaceOrientation theStatusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation];

if UIInterfaceOrientationIsLandscape(theStatusBarOrientation)
    keyboardShiftAmount = keyboardFrame.size.width;
else 
    keyboardShiftAmount = keyboardFrame.size.height;

[UIView beginAnimations: @"ShiftUp" context: nil];
[UIView setAnimationDuration: keyboardSlideDuration];
self.view.center = CGPointMake( self.view.center.x, self.view.center.y - keyboardShiftAmount);
[UIView commitAnimations];
viewShiftedForKeyboard = TRUE;
}
}

Upvotes: 0

Views: 218

Answers (2)

viggio24
viggio24

Reputation: 12366

In any case if you don't want to copy the full method in the subclass, and adding your little customization, the only other possible approach I see is to change the original class. To do it I can suggest two possibilities:

1) You could create in the original class a method called:

-(UITextField *)keyboardShiftTextField
and then in this class replace the
mirror.isEditing
code with:

[[self keyboardShiftTextField] isEditing]

In such case the only difference between the two classes will be in the implementation of the new method, that for the original class will be:

-(UITextField *)keyboardShiftTextField {
return mirror;
}
while in the subclass this return the right text field.

2) A second approach is more elegant as it requires the definition of the delegate pattern. This requires some overhead in term of code but we'll provide you more flexibility. Besides if the only reason to make the subclass is just to override this third method, then using the delegate pattern you can avoid creating the subclass at all, as the "custom" work will be done by the delegate. If the number of methods to override is more than one, you can still use this mechanism by moving into the protocol section all the parts that need customization. This is a quite common technique for Obj-C and Cocoa, which limits the need for some classes in many cases. Typically you use a subclass when you want to provide a different functionality, but in your case you're not providing a different functionality, but just a customization for the same functionality (the view shift up).

Upvotes: 2

Gareth McCaughan
Gareth McCaughan

Reputation: 19971

The usual approach would be the http://en.wikipedia.org/wiki/Template_method_pattern: pull out just the bit of the method that varies between classes, make that a separate method, and override that in subclasses.

[EDITED to add ...] An alternative approach -- I can't tell whether it would work well here without seeing more of your code -- would be to make the thing that varies a parameter that's passed into the method (or select it on the basis of a parameter passed into the method, or something else of the kind). (You'd typically then use other mechanisms rather than inheritance+polymorphism to get the effect you want, of multiple things with similar behaviour: they'd be instances of the same class but fed with different data.)

Upvotes: 1

Related Questions