32

I have too many views on one storyboard which is causing it to run really slow. I have been told that a solution to this issue would be to split the one storyboard into multiple storyboards. Could anyone tell me how I can segue from a view on storyboard 1 to a view in storyboard 2 via a button?

BrownEye
  • 979
  • 2
  • 12
  • 21

9 Answers9

35

With Xcode 7 you can pick Storyboard References

enter image description here

and set the destination storyboard and controller

enter image description here

Arnaud
  • 17,268
  • 9
  • 65
  • 83
27

In Swift (iOS 8.1) this is pretty easy:

  var storyboard: UIStoryboard = UIStoryboard(name: "Another", bundle: nil)
  var vc = storyboard.instantiateViewControllerWithIdentifier("NextViewController") as AnotherViewController
  self.showViewController(vc, sender: self)

Update for Swift 3:

let storyboard: UIStoryboard = UIStoryboard(name: "Another", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NextViewController") as! AnotherViewController
self.show(vc, sender: self)
Taiwosam
  • 469
  • 7
  • 13
metamorph2
  • 271
  • 3
  • 2
27

Adding a Reference to Another Storyboard - Apple developer doc

enter image description here

enter image description here

Community
  • 1
  • 1
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
9

Another solution using segues (iOS SDK 6.0+), which keeps code separated by purpose and leaves room for customisation:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    //check/validate/abort segue
    return YES;
}//optional

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    //sender - segue/destination related preparations/data transfer
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *destination = [[UIStoryboard storyboardWithName:@"SomeStoryboard" bundle:nil] instantiateInitialViewController];

    UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:@"identifier" source:self destination:destination performHandler:^(void) {
        //view transition/animation
        [self.navigationController pushViewController:destination animated:YES];
    }];

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [self shouldPerformSegueWithIdentifier:segue.identifier sender:cell];//optional
    [self prepareForSegue:segue sender:cell];

    [segue perform];
}

Note: UITableViewCell *cell is used as sender to keep default TableViewController response behaviour.

Nocross
  • 251
  • 1
  • 4
  • 6
  • Great solution Nocross, thanks! I see that your snippet does a "Push" segue. I adapted it to do a "Replace" by changing the code in the block to `(void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated` – jbbenni Feb 22 '14 at 21:13
6

I tried everything I had read but still had no success. I've managed to get it working using Rob Browns Storyboard Link It's easy to implement and works really fast

BrownEye
  • 979
  • 2
  • 12
  • 21
5

Finally XCode 7 has added this feature in which you can segue between view controllers in two different storyboards using interface builder. Till now, we had to do this programatically.

Udit Agarwal
  • 868
  • 9
  • 22
3

First of all, breaking up a storyboard into multiple separate ones is a great idea, saves a lot of headache (especially if you are on a team and dealing with lots of merge conflicts in the storyboard file).

Now to answer your question - you cannot perform a segue between two storyboards necessarily, but one solution I've had great success with is to do something like this:

- (IBAction)buttonPressed:(id)sender {

    UIViewController *otherVC = [[UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil] instantiateInitialViewController]; //Or get a VC by its identifier

    [self.navigationController pushViewController:otherVC animated:YES];
}

Just load up the other storyboard and either call instantiateInitialViewController or instantiateViewControllerWithIdentifier: then do whatever transition you would like.

Hope this helps.

Dan Fairaizl
  • 2,172
  • 2
  • 28
  • 31
  • So you just reference the storyboard and it will segue to the initial ViewController in that storyboard? – BrownEye Sep 13 '13 at 04:55
  • I have created two storyboards, placed a button on a VC on the first and made it buttonPressed. It doesn't link to the second one, the app just crashes. I'm still pretty new to Obj C, so i'm not sure if what i've done is even right. Is the code that you posted the only code that I need? – BrownEye Sep 13 '13 at 13:21
  • @BrownEye, it depends. Are you using a navigation controller in the first storyboard? That code just shows how to get a VC from the second storyboard. You cannot segue between the two, its a manual process. What is the crash you are getting? – Dan Fairaizl Sep 13 '13 at 14:14
  • I have a tableview embedded via a container in a normal ViewController, each guide is listed in the tableview and has its own set of views for each step. There is a tab bar style at the bottom of each page/step on the guide which is basically buttons to each of the 4 main pages. I would like to split the storyboard so each one has maybe 4 guides or one storyboard for each of our 4 main tab pages. Currently the way to get to each guide is via the tableview, and then they segue back via a button. I would like to be able to segue to a guide or the menu in a different storyboard. As it is slow – BrownEye Sep 13 '13 at 15:23
  • self.navigationController will be nil when the IBAction is set – Kesong Xie Aug 02 '16 at 23:48
2

Here is a simple Swift solution:

    let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController
    // .instantiatViewControllerWithIdentifier() returns AnyObject! this must be downcast to utilize it

    self.presentViewController(viewController, animated: false, completion: nil)
Mingebag
  • 748
  • 2
  • 20
  • 35
0

Swift 3

let vc = UIStoryboard(name: "StoryboardName", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerIdentifier") as? ExpectedViewControllerClass
self.show(vc, sender: self)

Where the "StroboardName" is the name of your .storyboardfile. The "ViewControllerIdentifier" is the id of the View in the story board. And "self" is any UIViewController

In my case, the identifier was "chooseCountryViewController"

al_mota
  • 431
  • 1
  • 4
  • 14