-1

I'm newbie to develop iOS program and I need your help.

When i click on the button, my code checks a condition, if the condition is true, I want to access to the next UIViewController passing data with prepareForSegue, but if the condition is FALSE, I don't want to access to the next VC and stay on the current VC.

How could i do that?

When i click on my button, I directly access to the next VC, whatever the code in the IBAction. Is it possible to block the access?

I try to code that on a push button

- (IBAction)lancepartie:(id)sender {
...
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143

3 Answers3

1

The other answers should work, but they kind of ignore the whole point of segues: you shouldn't have to manually set actions on buttons for things like segues. Instead, you should override the - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender selector.

To do that, you need to give your segue an identifier from Interface Builder, then add this code in your parent ViewController:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString*)identifier sender:(id)sender
{
    if ([identifier isEqualToString:@"MyIdentifier"])
    {
        return someCondition;
    }
    else return YES;
}
ahruss
  • 2,070
  • 16
  • 21
0
- (IBAction)lancepartie:(id)sender {
    BOOL someConditionHasBeenMet = NO;
    // check condition
    if (someConditionHasBeenMet) {
         // push view controller
    } else {
         // nothing
    }
}
runmad
  • 14,846
  • 9
  • 99
  • 140
0

try this code

- (IBAction)lancepartie:(id)sender
{
    if(Your_Condition_Is_True){
        [self performSegueWithIdentifier:@"Your_Segue_Identifier" sender:sender];
    }
    else {
        //do not switch controller
    }
}

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

    }
}
Gavin
  • 8,204
  • 3
  • 32
  • 42
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42