iOS 8: How to use segues

| | August 5, 2015

I am really confused about segues. There are so many different ways to change the current ViewController and I don’t know which to use and mix them up.

What I currently have, is a MainViewController. On this I have 4 Buttons. The first button is connected with a Show (e.g Push) Segue. On the other side of the Segue is a Navigation Controller with a View Controller. Now this works with no additional code.

Now my first Question. The Navigation Controller will never be shown, right? Only the View Controller will be shown. But if I want to add a Navigation Item Button on the right side to go back to the first View Controller how should I do it? I need to add the button programmatic like this:

UIBarButtonItem *leftBackToPreviousScreenButton = [[UIBarButtonItem alloc]
                                              initWithTitle:@""
                                              style:UIBarButtonItemStylePlain
                                              target:self
                                              action:@selector(back)];

- (void)back
{
    [self performSegueWithIdentifier:@"ExitEventOverview" sender:self];
}

Is this right?

Now the next step is, that I need to open another View to edit details of an event which are shown in the previous View as a table. When I click on the table row the Edit View should open and I need to pass the event object. How do I do this with segues?
After finishing editing how can I add programmatic a Cancel and Save button to the Edit View do return to the Overview of all Events?

2 Responses to “iOS 8: How to use segues”

  1. Don’t forget to implement a UINavigationController and put assign your UIViewController with 4 buttons as root view controller. Show segues presents the navigation bar but the others don’t, in that case you need to manage it and to add it.

    To show the details, just override the method prepareForSegue:sender to do so :

    [self performSegueWithIdentifier:@"NextSegue" sender:nil];
    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if([segue.identifier isEqualToString:@"NextSegue"]) {
            NextViewController *nextViewController = (NextViewController*)[segue destinationViewController];
            nextViewController.details = self.details;
            // nextViewController.details is the property "details" declared in your NextViewController.h file, you just give it the value you want to send from your present one.
        }
    }
    
  2. your coding is fine and add following line also

    UIBarButtonItem *leftBackToPreviousScreenButton = [[UIBarButtonItem alloc]
                                              initWithTitle:@""
                                              style:UIBarButtonItemStylePlain
                                              target:self
                                              action:@selector(back)];
    
    // add this line 
    self.navigationItem.rightBarButtonItem = leftBackToPreviousScreenButton;
    
    - (void)back
    {
    // if you want to pop on previous view controller
    
     [self.navigationController popViewControllerAnimated:YES];
    
    }
    

    in last question you need to customize ,but if you need to pass the data between two view controller use this link

Leave a Reply