简体   繁体   English

根据功能的返回值进行条件选择

[英]Conditional segue depending on return value of function

My parent viewcontroller has two secondary viewcontrollers (CriteriaViewController and MatchCenterViewController) that it can segue to. 我的父viewcontroller有两个可以辅助选择的辅助viewcontroller(CriteriaViewController和MatchCenterViewController)。

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". 我想做的是,如果“ eBayCategorySearch” cloudcode函数返回值“ 1”,则将其搜索到MatchCenter,如果返回值“ 0”,则将其搜索到CriteriaView。

I've attempted to set something up that does this, but it isn't functioning properly. 我试图设置一些可以做到这一点的工具,但是它不能正常工作。 How can I programatically setup this segue? 如何以编程方式设置此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. 如第一条评论中所述,您不应在prepareForSegue确定序列,因为它已经在那里确定。

You can create an IBAction for your nextButton and determine segue in it 您可以为nextButton创建IBAction并确定其中的顺序

- (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. 现在,您应该将下一个按钮与此操作相关联,并在IB中为您的任务设置标识符。

Also you can use prepareForSegue to transfer data between you view controllers 您也可以使用prepareForSegue在视图控制器之间传输数据

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowMatchCenterSegue"]) {
        MYMatchCenterViewController *matchCenterViewController = [segue destinationViewController];
        matchCenterViewController.data = self.someData;
    } else {
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM