本文介绍了prepareForSegue:如何设置if条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是非常新秀但... ...

This may be very rookie but...

我在两个 ViewControllers 之间设置了一个segue我的故事板,标识符 clickBtn

I've set a segue between two ViewControllers in my Storyboard, with the identifier clickBtn.

现在我在我的代码中用这种方式调用它:

Now I'm calling it this way in my code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"clickBtn"])
    {
        if(conditionVerified)
        {
            SecondViewController *controller = (SecondViewController *)segue.destinationViewController;
            [controller setManagedObjectContext:self.managedObjectContext];
        }
        else
        {
            // If I enter here I get the error below
        }
    }
}



事实上,如果我输入的话,我不想进行此转换别的语句。怎么做?

In fact I don't want to do this transition if I enter the else-statement. How to do so?

推荐答案

不是让按钮执行segue,而是调用类似下面的方法。

Rather than having the button perform the segue have it call a method like the following.

-(IBAction)mySegueButtonMethod:(id)sender
{
   if(conditionVerified){
      [self performSegueWithIdentifier:@"clickBtn" sender:sender];
   }
   else{
        // do not perform segue and do other relevant actions.
   }
}

您看到的错误是因为我假设控制器正在调用需要上下文管理器不要为零的东西。

The error you are seeing is because i am assuming controller is calling something that needs context manager not to be nil.

一旦进入方法prepareForSegue,就无法真正阻止segue的发生,因为它总是在方法结束时执行segue。

You can't really stop the segue from occurring once you are in the method prepareForSegue as it will always perform the segue at the end of the method.

这篇关于prepareForSegue:如何设置if条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:44