6

I'm a bit new to app development. In a viewController ( VPviewController ) i have the following code:

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    if (motion == UIEventSubtypeMotionShake){       
        [self startGame:nil];
    }   
}

In a different viewController ( VPgameViewController ) i have a different MotionShake event:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    if(event.subtype == UIEventSubtypeMotionShake){
        if(count < 3 ){

            [self changeText:nil];
            AudioServicesPlaySystemSound(1016);
            count++;

         }else{

            count = 0;
            AudioServicesPlaySystemSound(1024);
            UIStoryboard *storyboard = self.storyboard;
            VPpoepViewController *shit = [storyboard instantiateViewControllerWithIdentifier:@"PoepViewController"];
           shit.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
           [self presentViewController:shit animated:YES completion:nil];
        }
    }
}

When i'm in the VPgameView and i shake the Iphone it's also calling the startGame function which is in a different viewController shake event.

How can i stop this?

  • Maybe this (http://stackoverflow.com/questions/1342674/motionbegan-not-working) helps – basvk Nov 21 '12 at 16:04
  • In both views i have can become first responder and resign first responder. But that's not helping. – Wesley Van De Korput Nov 21 '12 at 18:26
  • Do you want to detect the motion in Xcode or in iOS? If the latter, please don't confuse iOS with Xcode. One doesn't need Xcode for writing iOS applications. –  Nov 21 '12 at 19:29

1 Answers1

2

Sounds like you have to unsubscribe your VPViewController from receiving the shake event notifications in its viewWillDisappear: function.

Generally, if you want your viewController to receive certain event notifications only when visible you should subscribe to the notification in the viewWillAppear: function and unsubscribe in the viewWillDisappear: function.

kadam
  • 1,259
  • 3
  • 14
  • 27
  • What is the best way to do this? – Wesley Van De Korput Nov 22 '12 at 09:40
  • 1
    To control if a viewController responds to a shake gesture you should introduce a `BOOL respondsToShakeGesture` class variable for the `viewController`. In the `motionBegan` and `motionEnded` functions return immediately if this bool value is false. Finally, in the `viewWillAppear:` function set the bool value to true and in the `viewWillDisappear:` function set the bool value to false. – kadam Nov 23 '12 at 18:00