2

I need to perform a segue from my storyboard. The method to call in that case is -[UIViewController performSegueWithIdentifier:sender:]. This method relies on the storyboard property of UIViewController to find the storyboard (and therefore the segue).

However, the storyboard property is not set when the UIViewController was not created from the storyboard. And since it's read-only, I can't set it programmatically where I load my storyboard.

So the question is: how to perform a segue from a storyboard that has been loaded programmatically?

If it's not possible, it's perhaps because my architecture is incorrect. Here is the use case:

The app is a legacy tabbar application where each of the 8 tabs has its own NIB file. Many of the 8 tabs are rather complex, and can benefit a lot from storyboards, especially prototype table cells and static tables. So I want to evolve the app to use storyboards.

However, one humongous storyboard doesn't seem a good idea: it would prevent incremental changes to the app, it would be unwieldy, it would make it difficult for the team members to work on their tab independently.

The right level of modularity seems to let the UITabBarController have its own specific storyboard. This makes it possible for each tab to evolve at its own pace and makes it easier for each developers to work on their tab with few source control conflicts.

My approach so far is the main nib file to contain the UITabBarController and each of its main daughter view controllers. The daughter view controllers load their storyboard from their viewDidLoad method. And from there, they can't perform their segue.

The alternative would be for the daughter view controllers to be created from their storyboards, but then how can I hook them up to the UITabBarController? And where do I programmatically load the storyboards?

Thanks for any suggestion.

Jean-Denis Muys
  • 6,772
  • 7
  • 45
  • 71
  • See [Using Multiple Storyboards in iOS][1]. [1]: http://stackoverflow.com/questions/8297701/using-multiple-storyboards-in-ios – T.J. Jan 06 '12 at 14:12

1 Answers1

0

make a reference on the segue and save the pointer value in a singleton class. Then using the pointer reference saved in the singleton class, access the segue wherever you like and load it. Here is a sample code in which i am loading a segue view from a single storyboard class but each view has separate view controllers .h and .m files that have appropiate connections. (i used formal protocol, hence the line shareVC.delegate = self; is there .

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue isKindOfClass:[UIStoryboardPopoverSegue class]]) {
if (self.currentPopover != nil) {
[_currentPopover dismissPopoverAnimated:YES];
 self.currentPopover = nil;
}

    UIStoryboardPopoverSegue *popSegue = (UIStoryboardPopoverSegue *)segue;
    self.currentPopover = popSegue.popoverController;
}

if ([segue.identifier compare:@"ShareModal"] == NSOrderedSame) {
    //the share controller is being presented modally, probably iphone

    UINavigationController *shareNavController = segue.destinationViewController;
    myViewController *shareVC = (myViewController *)[shareNavController topViewController];

    shareVC.delegate = self;


}  else if ([segue.identifier compare:@"SharePopover"] == NSOrderedSame) {


    FollowersViewController *followerVC = segue.destinationViewController;
    followerVC.delegate = self;



}  else if ([segue.identifier compare:@"StartScreenSegue"] == NSOrderedSame) {

UINavigationController *startNavController = segue.destinationViewController;
StartViewController *startVC = (StartViewController *)startNavController.topViewController;
startVC.delegate = self;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationOpenUrlNotification object:nil];
    }
}

Hope this helps:)

Izac Mac
  • 491
  • 3
  • 20
  • "make a reference on the segue" is precisely the issue. If I could do that, the view controller could do it when it needs it. Besides, I can't see what value a singleton will provide. Besides, I have plenty of segues, so would need a ton of singletons! Besides, singletons are evil anyway. – Jean-Denis Muys May 22 '12 at 13:26
  • I have solved the issue in a somewhat general way using a category on UITabBarController. My solution is probably worth posting somewhere. I'll write it up over the week-end – Jean-Denis Muys May 22 '12 at 13:33
  • do mention the link here. by the way . GREAT QUESTION :) – Izac Mac May 22 '12 at 13:41