Call storyboard scene programmatically (without needing segue)?

前端 未结 8 778
既然无缘
既然无缘 2020-12-04 10:10

I have a modal storyboard scene that I want to be accessible to all my other scenes. Creating a modal segue to it from every scene on my storyboard creates a big mess of str

8条回答
  •  温柔的废话
    2020-12-04 10:22

    I think that with iOS7 it has become very easy implementing via the storyboard

    I'm currently learning about the new features in iOS7 and found this simple solution, but it might have been relevant even in prior versions, I'm not sure.

    First u need to connect the presenting VC with the target VC (thats the only connection needed), then within the storyboard's attributes inspector choose the style to be modal, in the identity inspector give your VC a storyboardID and make sure you checked the 'use storyboardID' checkbox,

    If its not there yet add this method to your presentingVC:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
       YourTargetVC * targetVC = 
             (YourTargetVC *)segue.destinationViewController;
    
       if(nil != targetVC) {
    
           //Do preparations here
       }
    
    }
    

    Now, when you wish to show your targetVC from your presentingVC you can use:

    [self performSegueWithIdentifier:(NSString *) sender:(id)];
    

    where the identifier is your viewController's storyboardID, and the sender is the view who triggered the action, this method will invoke the storyboards scene, so the [prepareForSegue: sender:] method will be called allowing u making last modifications before the targetViewController will appear.

提交回复
热议问题