Segue from one storyboard to a different storyboard?

放肆的年华 提交于 2019-11-28 18:41:32

With Xcode 7 you can pick Storyboard References

and set the destination storyboard and controller

metamorph2

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)

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.

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

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.

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.

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)

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"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!