My parent viewcontroller has two secondary viewcontrollers (CriteriaViewController and MatchCenterViewController) that it can segue to.

What I want to do is to have it segue to MatchCenter if the "eBayCategorySearch" cloudcode function returns a value of "1", and to CriteriaView if it returns a value of "0".

I've attempted to set something up that does this, but it isn't functioning properly. How can I programatically setup this segue?

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if (sender != self.nextButton) return;

    if (self.itemSearch.text.length > 0) {

        [PFCloud callFunctionInBackground:@"eBayCategorySearch"
                           withParameters:@{@"item": self.itemSearch.text}
                                    block:^(NSString *result, NSError *error) {

                                        if (!error) {

                                            NSLog(@"The result is '%@'", result);

                                            if ([result intValue] == 1) {

                                                [self presentModalViewController:MatchCenterViewController
                                                                        animated:YES];
                                            }
                                            else {
                                                [self presentModalViewController:CriteriaViewController
                                                                        animated:YES];
                                            }
                                        }
                                    }];
    }
}
有帮助吗?

解决方案

As mentioned in a first comment you shouldn't determine a segue in prepareForSegue because it already determined there.

You can create an IBAction for your nextButton and determine segue in it

- (IBAction)nextButtonTapped:(id)sender
{
    if (self.itemSearch.text.length > 0) {
        [PFCloud callFunctionInBackground:@"eBayCategorySearch"
                           withParameters:@{@"item": self.itemSearch.text}
                                    block:^(NSString *result, NSError *error) {
                                        if (!error) {
                                            NSLog(@"The result is '%@'", result);

                                            if ([result intValue] == 1) {
                                                [self performSegueWithIdentifier:@"ShowMatchCenterSegue" sender:self];
                                            } else {
                                                [self performSegueWithIdentifier:@"ShowCriteriaSegue" sender:self];
                                            }
                                        }
                                    }];
    }
}

Now you should connect your next button with this action and set identifiers for your segues in IB.

Also you can use prepareForSegue to transfer data between you view controllers

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowMatchCenterSegue"]) {
        MYMatchCenterViewController *matchCenterViewController = [segue destinationViewController];
        matchCenterViewController.data = self.someData;
    } else {
        ...
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top