0
votes

I've made an application where you shake the phone to open a new view. All together three views, and when you shake the phone on the last view you go back to the first screen. This works fine when I'm creating new subclass controls view with their own .xib. But I would like to use this in a storyboard project, what do I need to change?

Thanks a lot on beforehand!

HERE IS THE CODE IN .H:

#import <UIKit/UIKit.h>
#import "FirstScreenViewController.h"
#import "SecondScreenViewController.h"

@interface ViewController : UIViewController

{

NSInteger currentScreen;
UIViewController* currentController;

}

@end

AND HERE IN THE .M:

#import "ViewController.h"

@implementation ViewController

-(void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.

}

#pragma mark shake

-(BOOL)canBecomeFirstResponder

{

return true;

}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

if(motion == UIEventSubtypeMotionShake)

{

if (currentController)

{

[currentController.view removeFromSuperview]; currentController=nil;

    }

    switch (currentScreen)

{

        case 0:
            currentController = [[FirstScreenViewController alloc] initWithNibName:@"FirstScreenViewController" bundle:nil];
            break;
        case 1:
            currentController = [[SecondScreenViewController alloc] initWithNibName:@"SecondScreenViewController" bundle:nil];

    }


    if(currentController)

{

        [currentController.view setFrame:self.view.bounds];
        [self.view addSubview:currentController.view];

    }

    currentScreen++;
    if(currentScreen >2)
        currentScreen=0;

}

}

#pragma mark - View lifecycle

-(void)viewDidLoad

{

[super viewDidLoad]; currentScreen = 0;

}

-(void)viewDidUnload

{

[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

}

@end

1
If you format your code correctly, you are more likely to get help.rob mayoff
Format code by pasting it in, selecting and then clicking the {} button above the question editor. Or, indent by 4 spaces.jrturton

1 Answers

1
votes

You need to add all three view controllers to the storyboard, and have segues between them (including one back to the first from the third) and a shake gesture recogniser attached to each scene.

The action method for each gesture recogniser tells the view controller to performSegue: with the appropriate segue identifier.