Search code examples
iosobjective-cuiviewcontrollerpresentviewcontroller

TouchesBegan recognised in next view controller


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];

    NSLog(@"touch recognised");

    CGPoint location = [aTouch locationInView:_mainView];

    __block NSString *option = [[NSString alloc] init];
    __block NSString *type = [[NSString alloc] init];

    for (Selectors *tempView in _mainView.subviews) {
        if (CGRectContainsPoint(tempView.frame, location)) {
            NSLog(@"%@ : %@", tempView.option, tempView.type);

            option = tempView.option;
            type = tempView.type;

            break;
        }
    }


    [self moveToNextWeldCustomViewWithOption:option andType:type];


}

Is in my previous UIVIewController - then we present the next UIViewController here

-(void)moveToNextWeldCustomViewWithOption:(NSString *)option andType:(NSString *)type {

    WeldDesignViewController *lobby = [self.storyboard instantiateViewControllerWithIdentifier:@"WeldDesignViewController"];

    lobby.option = option;
    lobby.type = type;

    [self presentViewController:lobby animated:NO completion:nil];

}

In the next UIViewController I don't do anything until the viewDidAppear method - However, the touches began is still being recognised in the next viewcontroller.


Solution

  • I think the issue is that the VC you are calling "lobby" is being deallocated after you present it. This takes it out of consideration in the responder chain. Move that reference to be a property of the presenting view controller:

    @property (nonatomic, strong) WeldDesignViewController* lobby;
    // ...
    -(void)moveToNextWeldCustomViewWithOption:(NSString *)option andType:(NSString *)type {
    
       self.lobby = [self.storyboard instantiateViewControllerWithIdentifier:@"WeldDesignViewController"];
    
        lobby.option = option;
        lobby.type = type;
    
        [self presentViewController:self.lobby animated:NO completion:nil];
    
    }
    

    You may also need to implement touchesBagan in the presented VC, as it is probably following the responder chain to the previous controller.