如何覆盖方法的一小部分?

时间:2012-04-28 21:08:56

标签: cocoa

我现在在课堂上有这些方法似乎工作得很好。我想创建一个继承这些方法的子类。我遇到的问题是在第三种方法(shiftViewUpForKeyboard)中我希望if语句是一个不同的UITextField(镜像是当前的例子)。 我已经读过要覆盖子类中的方法,你必须基本上用新编码复制它,但是如果我想改变那个小部分,那么最好的方法是什么呢? 提前谢谢。

- (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;
}
}

2 个答案:

答案 0 :(得分:2)

在任何情况下,如果您不想复制子类中的完整方法,并添加您的小自定义,我看到的唯一其他可能的方法是更改​​原始类。要做到这一点,我可以提出两种可能性:

1) 您可以在原始类中创建一个名为的方法:

-(UITextField *)keyboardShiftTextField
然后在此类中将
mirror.isEditing
代码替换为:

[[self keyboardShiftTextField] isEditing]

在这种情况下,两个类之间的唯一区别在于新方法的实现,对于原始类,将是:

-(UITextField *)keyboardShiftTextField {
return mirror;
}
而在子类中,这将返回正确的文本字段。

2) 第二种方法更优雅,因为它需要定义委托模式。这需要在代码方面有一些开销,但我们会为您提供更大的灵活性。此外,如果创建子类的唯一原因只是覆盖第三个方法,那么使用委托模式可以避免创建子类,因为“自定义”工作将由委托完成。如果要覆盖的方法数量多于一个,您仍然可以通过将所有需要自定义的部分移入协议部分来使用此机制。对于Obj-C和Cocoa来说,这是一种非常常见的技术,在许多情况下限制了对某些类的需求。通常,当您想要提供不同的功能时,您会使用子类,但在您的情况下,您不是提供不同的功能,而是仅提供相同功能的自定义(视图向上移动)。

答案 1 :(得分:1)

通常的方法是http://en.wikipedia.org/wiki/Template_method_pattern:只取出类之间不同的方法,使其成为一个单独的方法,并在子类中重写它。

[已编辑添加...]另一种方法 - 我无法在没有看到更多代码的情况下判断它是否可以正常运行 - 这样可以使参数更改为&#39 ; s传入方法(或根据传递给方法的参数或类似的其他东西选择它)。 (您通常会使用其他机制而不是继承+多态来获得您想要的效果,具有相似行为的多个事物:它们是同一类的实例,但却提供不同的数据。)

相关问题