Creating a segue programmatically

前端 未结 13 972
名媛妹妹
名媛妹妹 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:05

    You have to link your code to the UIStoryboard that you're using. Make sure you go into YourViewController in your UIStoryboard, click on the border around it, and then set its identifier field to a NSString that you call in your code.

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" 
                                                         bundle:nil];
    YourViewController *yourViewController = 
     (YourViewController *)
      [storyboard instantiateViewControllerWithIdentifier:@"yourViewControllerID"];
    [self.navigationController pushViewController:yourViewController animated:YES];
    
    0 讨论(0)
  • 2020-11-22 13:10

    I thought I would add another possibility. One of the things you can do is you can connect two scenes in a storyboard using a segue that is not attached to an action, and then programmatically trigger the segue inside your view controller. The way you do this, is that you have to drag from the file's owner icon at the bottom of the storyboard scene that is the segueing scene, and right drag to the destination scene. I'll throw in an image to help explain.

    enter image description here

    A popup will show for "Manual Segue". I picked Push as the type. Tap on the little square and make sure you're in the attributes inspector. Give it an identifier which you will use to refer to it in code.

    enter image description here

    Ok, next I'm going to segue using a programmatic bar button item. In viewDidLoad or somewhere else I'll create a button item on the navigation bar with this code:

    UIBarButtonItem *buttonizeButton = [[UIBarButtonItem alloc] initWithTitle:@"Buttonize"
                                                                        style:UIBarButtonItemStyleDone
                                                                       target:self
                                                                       action:@selector(buttonizeButtonTap:)];
    self.navigationItem.rightBarButtonItems = @[buttonizeButton];
    

    Ok, notice that the selector is buttonizeButtonTap:. So write a void method for that button and within that method you will call the segue like this:

    -(void)buttonizeButtonTap:(id)sender{
        [self performSegueWithIdentifier:@"Associate" sender:sender];
        }
    

    The sender parameter is required to identify the button when prepareForSegue is called. prepareForSegue is the framework method where you will instantiate your scene and pass it whatever values it will need to do its work. Here's what my method looks like:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"Associate"])
        {
            TranslationQuizAssociateVC *translationQuizAssociateVC = [segue destinationViewController];
            translationQuizAssociateVC.nodeID = self.nodeID; //--pass nodeID from ViewNodeViewController
            translationQuizAssociateVC.contentID = self.contentID;
            translationQuizAssociateVC.index = self.index;
            translationQuizAssociateVC.content = self.content;
        }
    }
    

    Ok, just tested it and it works. Hope it helps you.

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

    First of, suppose you have two different views in storyboard, and you want to navigate from one screen to another, so follow this steps:

    1). Define all your views with class file and also storyboard id in identity inspector.

    2). Make sure you add a navigation controller to the first view. Select it in the Storyboard and then Editor >Embed In > Navigation Controller

    3). In your first class, import the "secondClass.h"

    #import "ViewController.h
    #import "secondController.h"
    

    4). Add this command in the IBAction that has to perform the segue

    secondController *next=[self.storyboard instantiateViewControllerWithIdentifier:@"second"];
    [self.navigationController pushViewController:next animated:YES];
    

    5). @"second" is secondview controller class, storyboard id.

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

    For controllers that are in the storyboard.

    jhilgert00 is this what you were looking for?

    -(IBAction)nav_goHome:(id)sender {
    UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"];
    [self.navigationController pushViewController: myController animated:YES];
    
    }
    

    OR...

    [self performSegueWithIdentifier:@"loginMainSegue" sender:self];
    
    0 讨论(0)
  • 2020-11-22 13:19

    I've been using this code to instantiate my custom segue subclass and run it programmatically. It seems to work. Anything wrong with this? I'm puzzled, reading all the other answers saying it cannot be done.

    UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OtherViewControllerId"];
    MyCustomSegue *segue = [[MyCustomSegue alloc] initWithIdentifier:@"" source:self destination:toViewController];
    [self prepareForSegue:segue sender:sender];
    [segue perform];
    
    0 讨论(0)
  • 2020-11-22 13:21

    A couple of problems, actually:

    First, in that project you uploaded for us, the segue does not bear the "segue1" identifier:

    no identifier

    You should fill in that identifier if you haven't already.

    Second, as you're pushing from table view to table view, you're calling initWithNibName to create a view controller. You really want to use instantiateViewControllerWithIdentifier.

    0 讨论(0)
提交回复
热议问题