0

ウィンドウに単一の UIScrollView を追加するベアボーン iOS アプリケーションを作成しました (ViewControllers または UIViews はありません。何が起こっているのかを理解できるかどうかを確認するためにこれを行いました)。

私が欲しいのは、デバイスが揺れたときに UIScrollView に通知することだけです。

canBecomeFirstResponder メソッドを実装するために UIScrollView をサブクラス化しました。

#import "SampleScrollView.h"

@implementation SampleScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

そして、AppDelegate で、SampleScrollView のオブジェクトを作成し、ウィンドウに追加してから、最初のレスポンダーに設定しようとします。

#import "SampleScrollView.h"

@implementation ResponderTestAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    // Override point for customization after application launch.

    CGRect thisFrame = [[self window] bounds];
    SampleScrollView *myScrollView = [[SampleScrollView alloc] initWithFrame:thisFrame];

    // Set scrollview to be the first responder
    BOOL success = [myScrollView becomeFirstResponder];
    if (success)
    {
        NSLog(@"myScrollView is first responder");
    }
    else
    {
        NSLog(@"Not first responder.");
    }

これは非常に単純化された例ですが、何らかの理由でアプリケーションに SampleScrollView オブジェクトをファーストレスポンダーとして報告させることができません。私は非常に単純なものが欠けていると確信していますよね?

4

1 に答える 1

0

あなたの例では、スクロールビューをスーパービューに追加していません。[self.window addSubview: myScrollView];を呼び出す前に、この行を追加してみてくださいbecomeFirstResponder

ドキュメントには、次のbecomeFirstResponderように記載されています

このメソッドを呼び出して、ビューなどのレスポンダー オブジェクトをファーストレスポンダーにすることができます。ただし、ビュー階層の一部である場合にのみ、そのビューで呼び出す必要があります。ビューの window プロパティが UIWindow オブジェクトを保持している場合、それはビュー階層にインストールされています。nil を返す場合、ビューは階層から切り離されます。

于 2013-02-11T17:55:44.507 に答える