Prashant Sharma Prashant Sharma - 1 year ago 93
iOS Question

Segue is executed twice

I am trying to transition between view controllers with a segue. It is navigating correctly but it is running twice.

Here is my code:

- (IBAction)onActionNext:(id)sender {
if ([[nameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]isEqualToString:@""]) {

}else if([[emailField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""]){

}else if([[mobileField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""]){

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

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"VerifyNext"]) {

// Get destination view
OTPSceneViewController *vc = [segue destinationViewController];
[vc setTitle:@"myapp"];

Answer Source

If even after commenting this line segue still run then as @mikealter said segue is connected your button. Check if when you check for when you click segue what it highlights.

A button like:

When Segue is connected to a button

Or A View Controller like:

When Segue is connected to a viewController

If you fall in first case that means your segue is connected to a button and, that is the reason why it opens next page even if you comment your code. And 2 times when you keep code(one from storyboard and another from code).

Edit: If you want to call next page from code only. Remove Segue. Then reconnect it with view controller like:

enter image description here