Search code examples
iosbuttonstoryboardibaction

Xcode (iOS) - Switching Between Storyboards With The Press Of A Button


This is a very simple question, I want to know how I'd switch between storyboards with the press of a button. I understand how to get input from a button.

- (IBAction)button:(id)sender {
     //Code goes here
}

All I need to know though, is the code that'll allow me to switch between storyboards.

Any help is appreciated.


Solution

  • Define a enum like this:

    typedef NS_ENUM(NSInteger, StoryBordType) {
        StoryBordTypeA = 0,
        StoryBordTypeB,
        StoryBordTypeC
    };
    

    Then you can define couple of class level properties:

    @property (nonatomic, strong) UIStoryboard *storyboard;
    @property (nonatomic, assign) StoryBordType currentStoryBoard;
    

    Finally, implement your button action handler like this:

    - (IBAction)button:(id)sender {
        switch (self.currentStoryBoard) {
            case StoryBordTypeA:
                self.storyboard = [UIStoryboard storyboardWithName:@"StoryBordTypeB" bundle:nil];
                self.currentStoryBoard = StoryBordTypeB;
                break;
    
            case StoryBordTypeB:
                self.storyboard = [UIStoryboard storyboardWithName:@"StoryBordTypeC" bundle:nil];
                self.currentStoryBoard = StoryBordTypeC;
                break;
    
            case StoryBordTypeC:
                self.storyboard = [UIStoryboard storyboardWithName:@"StoryBordTypeA" bundle:nil];
                self.currentStoryBoard = StoryBordTypeA;
                break;
    
            default:
                break;
        }
    }