2012-04-28 21 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

相關問題