2
votes

I am new to storyboard and xcode (using 4.6) I m using Navigation Storyboard with push segue. I would like to implement a select snap view, that if have minimum 1 snap selected - the segue would work. Otherwise, the user should stay on the same view.

I created 2 UIView: SelectSnapsViewController and ShippingDetailViewController.

In SelectSnapsViewController.m I added the following:

- (IBAction)nextButtonPressed:(id)sender
{
    looseDataAlert = NO;
    [okkkBtn setFrame:CGRectMake(116, 272, 72, 37)];
    if ([appDelegate.selImageDetails count] == 0)
    {
        label1.text = @"To proceed you need to add snaps.";
        [self openAlertView];
    }
    else{
        [self performSegueWithIdentifier:@"MySegue" sender:self];
    }
}

When I debug the code - I see that the it always fall in else case of condition, however the segue (transition to the new view) still happens.

How can I prevent the user to be transferred to the new view?

2
You need to setup the Segue as a 'manual' segue from SelectSnapsViewController rather than a triggered segue from your button.Mike Pollard
This tutorial appcoda.com/… might help you.Prasad Devadiga
@MikePollard: could you explain me step by step.Wise

2 Answers

3
votes

There are two ways to implement conditional segues.

1) Configure the Segues as 'manual' from the ViewController and invoke performSegueWithIdentifier conditionally within your IBAction method that you wire up to event handling for your button.

OR 2) Configure the Segue as a 'Triggered Segue' from your button and implement - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender in your view controller to perform the conditional logic.

1
votes

Are you saying that you only want to perform the segue if a condition is true?

If so, instead of creating the segue directly from a control or table cell, create a triggerless segue. A triggerless segue has the view controller as its source, and it won't ever fire automatically. Instead you can fire it programmatically any time you like, including from an IBAction.

To create a triggerless segue, start control+dragging the segue from the containing view controller icon in the scene dock at the bottom of the scene. Drag to the destination scene like normal, and pick the segue type. Select the segue, and in the inspector, choose a segue identifier.

At runtime, when you want to perform the segue, invoke -[UIViewController performSegueWithIdentifier:sender:]. You can pass any object you'd like for the sender, including nil. If the sender has no use to you, pass nil.

So, in summary:

Create a triggerless segue from the view controller to the destination scene
Set an identifier for the segue in the inspector
At runtime, and form code, call -[UIViewController performSegueWithIdentifier:sender:] when you want to trigger the segue.