Question

I'm developing an app in Xamarin Studio (so it's C#) and it has a form with multiple TextFields , I want to validate the whole screen before the segue to the next screen is performed. It is working with the following code when the button is clicked:

saveButton.Clicked += delegate(object sender, EventArgs e) {

    if(ValidateScreen()){
        PerformSegue("nextScreen", this);
    }
};

Now I'm wondering if this is the best way to go or should I use ShouldPerformSegue, something like this:

saveButton.Clicked += (object sender, EventArgs e) => ShouldPerformSegue ("nextScreen", this);

And the ShouldPerformSegue method:

public override bool ShouldPerformSegue (string segueIdentifier, NSObject sender)
{
    bool validScreen = ValidateScreen ();
    bool result = true;
    if (!validScreen) {
        result = false;
    }
    return result;
}

So what is the best way to go? Why would I use a lot more code with ShouldPerformSegue if my current code is also working, but then why is there a ShouldPerformSegue method?

(PS. the ShouldPerformSegue isn't working yet, does anybody know why? Not the main question..)

Was it helpful?

Solution

In this case maybe the documentation for ShouldPerformSegue is slightly vague.

You normally want to override and use ShouldPerformSegue when you want to say abort a segue that is invoked via the Storyboard.

enter image description here

In the above image's project we got the Button lead to the VC titled "Second" via a push segue in the Storyboard. Now we could use the ShouldPerformSegue function in code to control when this segue from the storyboard should actually perform the transition to the "Second" VC by returning true/false from that function.

You'll also probably use the segue identifier at those points to identify the segue that's being handled currently(provided you set an identifier for the segue in the storyboard) if you have multiple segue's from the storyboard for that VC.

In your case you're invoking PerformSegue("nextScreen", this); from code, so in essence you would know when you should actually invoke it(using your ValidateScreen()). Thus you can safely ignore the override and just proceed as you initially had.

OTHER TIPS

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender

{ return YES/NO; }

you can use upper method to validate your segue just return YES if your all validation are corret or else you can return NO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top