Creating a segue programmatically

前端 未结 13 973
名媛妹妹
名媛妹妹 2020-11-22 12:41

I have a common UIViewController that all my UIViewsControllers extend to reuse some common operations.

I want to set up a segue on this \"

相关标签:
13条回答
  • 2020-11-22 13:23

    I reverse-engineered and made an open source (re)implementation of UIStoryboard's segues: https://github.com/acoomans/Segway

    With that library, you can define segues programmatically (without any storyboard).

    Hope it may help.

    0 讨论(0)
  • 2020-11-22 13:25

    By definition a segue can't really exist independently of a storyboard. It's even there in the name of the class: UIStoryboardSegue. You don't create segues programmatically - it is the storyboard runtime that creates them for you. You can normally call performSegueWithIdentifier: in your view controller's code, but this relies on having a segue already set up in the storyboard to reference.

    What I think you are asking though is how you can create a method in your common view controller (base class) that will transition to a new view controller, and will be inherited by all derived classes. You could do this by creating a method like this one to your base class view controller:

    - (IBAction)pushMyNewViewController
    {
        MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
    
        // do any setup you need for myNewVC
    
        [self presentModalViewController:myNewVC animated:YES];
    }
    

    and then in your derived class, call that method when the appropriate button is clicked or table row is selected or whatever.

    0 讨论(0)
  • 2020-11-22 13:25

    well , you can create and also can subclass the UIStoryBoardSegue . subclassing is mostly used for giving custom transition animation.

    you can see video of wwdc 2011 introducing StoryBoard. its available in youtube also.

    http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIStoryboardSegue_Class/Reference/Reference.html#//apple_ref/occ/cl/UIStoryboardSegue

    0 讨论(0)
  • 2020-11-22 13:26

    Storyboard Segues are not to be created outside of the storyboard. You will need to wire it up, despite the drawbacks.

    UIStoryboardSegue Reference clearly states:

    You do not create segue objects directly. Instead, the storyboard runtime creates them when it must perform a segue between two view controllers. You can still initiate a segue programmatically using the performSegueWithIdentifier:sender: method of UIViewController if you want. You might do so to initiate a segue from a source that was added programmatically and therefore not available in Interface Builder.

    You can still programmatically tell the storyboard to present a view controller using a segue using presentModalViewController: or pushViewController:animated: calls, but you'll need a storyboard instance.

    You can call UIStoryboards class method to get a named storyboard with bundle nil for the main bundle.

    storyboardWithName:bundle:

    0 讨论(0)
  • 2020-11-22 13:27

    Guess this is answered and accepted, but I just would like to add a few more details to it.

    What I did to solve a problem where I would present a login-view as first screen and then wanted to segue to the application if login were correct. I created the segue from the login-view controller to the root view controller and gave it an identifier like "myidentifier".

    Then after checking all login code if the login were correct I'd call

    [self performSegueWithIdentifier: @"myidentifier" sender: self];
    

    My biggest misunderstanding were that I tried to put the segue on a button and kind of interrupt the segue once it were found.

    0 讨论(0)
  • 2020-11-22 13:27

    Here is the code sample for Creating a segue programmatically:

    class ViewController: UIViewController {
        ...
        // 1. Define the Segue
        private var commonSegue: UIStoryboardSegue!
        ...
        override func viewDidLoad() {
            ...
            // 2. Initialize the Segue
            self.commonSegue = UIStoryboardSegue(identifier: "CommonSegue", source: ..., destination: ...) {
                self.commonSegue.source.showDetailViewController(self.commonSegue.destination, sender: self)
            }
            ...
        }
        ...
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // 4. Prepare to perform the Segue
            if self.commonSegue == segue {
                ...
            }
            ...
        }
        ...
        func actionFunction() {
            // 3. Perform the Segue
            self.prepare(for: self.commonSegue, sender: self)
            self.commonSegue.perform()
        }
        ...
    }
    
    0 讨论(0)
提交回复
热议问题