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;

}

よろしくお願いします!

4

1 に答える 1

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、通知ハンドラを呼び出す代わりに呼び出します。

メソッドを直接呼び出す場合は、スライドの長さとフレームを自分で渡す必要があります。

于 2012-04-28T08:02:18.433 に答える