xcode ios 6 shake motion calls IBaction from previous view
Asked Answered
D

1

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?

Dyspepsia answered 21/11, 2012 at 15:18 Comment(3)
Maybe this (https://mcmap.net/q/800146/-motionbegan-not-working) helpsWelterweight
In both views i have can become first responder and resign first responder. But that's not helping.Dyspepsia
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.Internationale
A
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.

Alix answered 21/11, 2012 at 19:26 Comment(2)
What is the best way to do this?Dyspepsia
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.Alix

© 2022 - 2024 — McMap. All rights reserved.