如果不满足条件,则 Xcode 故事板取消推送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9044265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:08:19  来源:igfitidea点击:

Xcode storyboard cancel push if a condition is not met

xcodeuiviewcontrolleruinavigationcontrollerpushstoryboard

提问by H Bellamy

I have a storyboard with a button in a Navigation View Control, this control has textfields in as well, which uses the storyboard segue push. However, I have some input checking, and I alert the user if any of the text fields are empty.

我在导航视图控件中有一个带有按钮的情节提要,该控件也有文本字段,它使用情节提要 segue push。但是,我有一些输入检查,如果任何文本字段为空,我会提醒用户。

I can do this, BUT it alerts the user, and then switches to the other view.

我可以这样做,但它会提醒用户,然后切换到另一个视图。

Is there a way to cancel this pushif an if condition is not met in the button Pressed method?

push如果在按钮 Pressed 方法中不满足 if 条件,有没有办法取消它?

N.B This push was created by the storyboard interface, not by code.

注意这个推送是由故事板界面创建的,而不是由代码创建的。

回答by T.J.

Don't create the segue by dragging from the button to the destination controller. Instead, just drag from the source controller to the destination controller to create a segue. Click on the segue and use the attribute inspector to specify the segue is a push and give the segue an identifier (goToDestination). Then connect the button to an IBAction in the source view controller. In the IBAction method do your checking and if you want to do the push add this line of code:

不要通过从按钮拖动到目标控制器来创建转场。相反,只需从源控制器拖动到目标控制器即可创建转场。单击 segue 并使用属性检查器指定 segue 是推送并为 segue 提供标识符 (goToDestination)。然后将按钮连接到源视图控制器中的 IBAction。在 IBAction 方法中进行检查,如果要进行推送,请添加以下代码行:

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

回答by Vishal Chaudhry

The following can also be used and will work for different types of segues like push, unwind, etc. Name the segue from the Storyboard and use the code given here. Return NO for the condition if you do not want the particular segue to be performed and return YES by default. This method should be in the source view controller.

也可以使用以下内容,并将适用于不同类型的转场,如推送、展开等。从故事板中命名转场并使用此处给出的代码。如果您不希望执行特定的 segue,则为条件返回 NO,默认情况下返回 YES。这个方法应该在源视图控制器中。

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    if ([identifier isEqualToString:@"Segue_Name"]) {
        NSLog(@"Segue Blocked");
        //Put your validation code here and return YES or NO as needed
        return NO;
    }

    return YES;
}

回答by Nimmy Alphonsa Jose

If buttonAction is the method that have to execute on button click you have to call two methods inside it 1. Method to check the conditions 2. Method to control the segue

如果 buttonAction 是必须在按钮单击时执行的方法,则必须在其中调用两个方法 1. 检查条件的方法 2. 控制 segue 的方法

 - (IBAction)buttonAction:(id)sender
     {
     [self validateEnteredData];
     [self shouldPerformSegueWithIdentifier:kSegueName sender:sender];
     }

Your validation method will be looking as follows

您的验证方法将如下所示

-(void)validateEnteredData
 {
if (self.usernameTextField.text.length==0 && self.passwordTextField.text.length==0)
{
    UIAlertView * alert=[[UIAlertView alloc]initWithTitle:kError message:kInvalidationMessage delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    return;
}
else if (![self.usernameTextField.text isEqualToString:@"admin"] && ![self.passwordTextField.text isEqualToString:@"pass"])
{
    UIAlertView * alert=[[UIAlertView alloc]initWithTitle:kError message:kLoginFailed delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    return;
}
}

and segue can be controlled by the following code segment

和segue可以通过以下代码段控制

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if ([identifier isEqualToString:kSegueName])
{
//Validation: Return NO if you don't want to execute the segue action        
    if (self.usernameTextField.text.length==0 && self.passwordTextField.text.length==0)
    {
        return NO;
    }
    else if (![self.usernameTextField.text isEqualToString:@"admin"] && ![self.passwordTextField.text isEqualToString:@"pass"])
    {
        return NO;
    }
 }

  return YES;
}