5

次のコード セットを使用して、View Controller の戻るジェスチャを無効にしようとしています。

ではFirstViewController.m、デリゲートを設定していますinteractivePopGestureRecognizer

- (void) viewWillLoad {

    // Other stuff..
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

そして、<UIGestureRecognizerDelegate>メソッドを実装して返しNOます。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

     return NO;
}

そして、dealloc ではデリゲートを nil に設定しています。(iOS 7 ではデリゲートを手動で nil に設定する必要があることをどこかで読んだことがあります)

- (void)dealloc {

    self.navigationController.delegate = nil;
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}

これは で機能しFirstViewControllerます。しかし、SecondViewControllerこれにプッシュすると、ジェスチャも機能しません。FirstViewController のみでジェスチャを無効にするにはどうすればよいですか?

また、ポップFirstViewControllerしてから再度RootViewControllerプッシュしようとするとFirstViewController、オブジェクトの割り当て解除エラーが発生します。

[FirstViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 0x14ed0280

デリゲートを nil に設定する以外に、他に何をする必要があるのでしょうか? それとも、間違った場所に設定していますか?

4

6 に答える 6

24

FirstViewController で以下のテストされていないコードを試してください。

-(void) viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

-(void) viewWillDisappear:(BOOL)animated 
{
    [super viewWillDisappear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
于 2013-10-01T05:38:56.583 に答える
3

上記のすべてを試しましたが、うまくいきませんでした。

ビュー コントローラーがこのプロトコル、つまりUIGestureRecognizerDelegate を実装していることを確認し、以下のコードを記述してください。

-(void)viewWillAppear : (BOOL) アニメーション {

[super viewWillAppear : animated];

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

    self.navigationController.interactivePopGestureRecognizer.enabled =

いいえ;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {

    return NO;

} else {

    return YES;

}

}

于 2014-12-19T11:13:29.937 に答える
0

これはxCode 7でうまくいきました:

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController!.interactivePopGestureRecognizer!.enabled = false

}
override func viewWillDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
    self.navigationController!.interactivePopGestureRecognizer!.enabled = true
}
于 2015-09-14T18:29:37.353 に答える