2012-04-28 7 views
0

私はクラス内にこのメソッドを持っています。 [self shiftViewUpForKeyboard]を呼び出すと(このクラスの)サブクラスでどのように使用するのですか。それは引数を必要としますが、私がTheNotificationとタイプするとエラーになります。私はこれがおそらく非常に基本的だと知っていますが、それは本当に私のアプリ全体を通してたくさんの助けになります。サブクラス化されたメソッドを使用する方法

- (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 _];'を試しましたか? –

答えて

3

これは通知ハンドラのようです。一般的に、通知ハンドラは自分で呼び出さないでください。通知ハンドラメソッドは通常、NSNotificationCenterによって発行された通知によって呼び出されます。通知センターはNSNotificationオブジェクトをハンドラメソッドに送信します。あなたの場合、通知には追加のユーザー情報が含まれています。

ハンドラーを直接呼び出す必要のあるユーザー情報辞書をコード内で似て、ハンドラーメソッドに渡す必要があります(必要なユーザー情報辞書で独自のNSNotificationオブジェクトを構築する)。しかし、それはエラーが発生しやすく、私はそれを「ハック」と見なします。

コードを別個のメソッドに置き、質問からの通知ハンドラからそのメソッドを呼び出し、直接呼び出しにdistinctメソッドを使用することをお勧めします。

その後、必要があります:

- (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、私はそれを後で試してみよう! –

関連する問題