2012-04-28 82 views
0

我在这个类中有这个方法。我如何在我的子类(这个类)中使用它,因为当我调用[self shiftViewUpForKeyboard]时;它需要参数,但是当我输入通知时,它会给出错误。我知道这可能是非常基本的,但是在整个我的应用程序中它确实会帮助我很多。如何使用子类方法

- (void) shiftViewUpForKeyboard: (NSNotification*) theNotification; 
{ 


    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; 

} 

谢谢亲切!

+1

你试过了吗?[self shiftViewUpForKeyboard:_theVariableYouWantToPass _];'? – 2012-04-28 08:05:42

回答

3

这看起来像通知处理程序。通常你不应该自己调用通知处理程序。通知处理程序方法通常由NSNotificationCenter发出的通知调用。通知中心将NSNotification对象发送给处理程序方法。在你的情况下,通知包含一些额外的用户信息。

您可能类似于代码中的用户信息字典,应直接调用处理程序并将其传递给处理程序方法(使用所需的用户信息字典构建自己的NSNotification对象)。然而,那会很容易出错,我会认为这是一个'黑客'。

我建议你把你的代码放到一个不同的方法中,从你的问题的通知处理程序中调用该方法,然后使用不同的方法进行直接调用。

你将不得不:

- (void) shiftViewUpForKeyboard: (NSNotification*) theNotification; 
{ 
    NSDictionary* userInfo = theNotification.userInfo; 
    keyboardSlideDuration = [[userInfo objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue]; 
    keyboardFrame = [[userInfo objectForKey: UIKeyboardFrameBeginUserInfoKey] CGRectValue]; 
    [self doSomethingWithSlideDuration:keyboardSlideDuration frame:keyboardFrame]; 
} 

落实doSomethingWithSlideDuration:frame:方法,你的类的实例方法。在直接拨打电话的代码中,请拨打doSomethingWithSlideDuration:frame而不是调用通知处理程序。

当您直接调用方法时,您需要自行传递幻灯片持续时间和帧。

+0

谢谢@starbugs,我会稍后再试! – 2012-04-28 08:06:05

相关问题