简体   繁体   English

performSegueWithIdentifier:导致应用崩溃

[英]performSegueWithIdentifier: causes app to crash

I have a button that is pressed after user enters email information. 用户输入电子邮件信息后,我有一个按下的按钮。 I have an alert view that is displayed when there is no email entered but if there is I want the button to segue to another view controller. 我有一个警报视图,该警报视图在没有输入电子邮件但如果我希望将按钮连接到另一个视图控制器时显示。

The following code causes my app to crash. 以下代码导致我的应用崩溃。 I have no idea why. 我不知道为什么。 Please help. 请帮忙。

(note: I have tried "sender:self]" "sender:nil]" and "sender:sender]" and they all make my app crash.) (注意:我尝试过“ sender:self]”,“ sender:nil]”和“ sender:sender]”,它们都使我的应用崩溃。)

- (IBAction)nextButtonPushed:(id)sender {

    if ([self.emailTextField.text  isEqual: @""]) {

        emailAlertView = [[UIAlertView alloc] initWithTitle:@"Missing Email" message:@"A destination email is required to send." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [emailAlertView show];
        self.nextButton.enabled = NO;
    }

    else {

        eString = self.eTextField.text;
        hString = self.hField.text;
        emailAddress = self.emailTextField.text;

        [userDefaults setObject:eString forKey:@"e"];

        [userDefaults setObject:hString forKey:@"h"];

        [userDefaults setObject:emailAddress forKey:@"email"];

        [self performSegueWithIdentifier:@"next" sender:self];
    }

}

There are three overwhelmingly likely possibilities: 共有三种可能性:

  • Perhaps the storyboard really has no segue named "next" emerging from the FirstViewController scene. 也许故事板确实没有从FirstViewController场景中出现的名为“ next”的标记。 Be careful: spaces and capitalization and things like that matter. 注意:空格和大小写以及类似的内容很重要。

  • Perhaps the storyboard has a segue named "next" but it emerges from a different scene (a different view controller). 故事板可能有一个名为“ next”的脚本,但它是从不同的场景(不同的视图控制器)中出现的。

  • Perhaps the FirstViewController instance represented by self in your code is not the same as the FirstViewController instance in the storyboard, ie maybe it came into existence in some other way and not by instantiation from the storyboard. 也许在代码中由self表示的FirstViewController实例与情节提要中的FirstViewController实例不同,即,它可能以其他某种方式而不是通过从情节提要中实例化而出现。 You might even have two FirstViewController objects in the storyboard, and the segue comes from the other one. 您甚至可能在情节提要中有两个FirstViewController对象,并且segue来自另一个对象。

One great way to figure out why your app is crashing on a line like this is to disable any breakpoints you may have (including exception breakpoints). 弄清楚您的应用为何在这样的行上崩溃的一种好方法是禁用您可能拥有的任何断点(包括异常断点)。 This will cause the app to crash and often tell you a reason why it is crashing. 这将导致应用崩溃,并经常告诉您崩溃的原因。 Normally you'll get error's like @matt mentioned such as bad identifiers on your seques (often typing errors), multiple of the same object in an IB scene such as the following: 通常,您会得到类似@matt的错误,例如序列号上的错误标识符(通常是输入错误),IB场景中多个相同对象的错误,例如:

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<YOURAPP.YOURVIEWCONTROLLER: 0x7c8951d0>) has no segue with identifier 'TheSequeIdentifierYouHaventSetYetOrTypedInWrong''

or like in my case today where I got this error: 或像今天这样的错误

 Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<YOURAPP.YOURVIEWCONTROLLERYOUARETRYINGTOSHOW 0x78e5a140> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key "MyKeyOnTheOffendingObject"

Once I knew that I had an object in my storyboard with a key that it should not have all I had to do was track down the Button/Image/Other Random Object with the bad Referencing Outlet or Sent Event and all was fixed 一旦我知道情节提要中有一个带有键的对象,而该键不应该我要做的就是跟踪具有不良引用出口或已发送事件的Button / Image / Other Random对象,并且所有这些都已修复

Often performSequeWithIdentifier will come up as the last line shown before a crash since that is the last line that worked before a problem was found when presenting a new View Controller - so if you are sure the problem isn't with your seque then check references on the VC you are trying to present! 通常, performSequeWithIdentifier会出现在崩溃之前的最后一行,因为这是在展示新的View Controller时发现问题之前可以使用的最后一行-因此,如果您确定问题不在您的定居中,请检查引用您要呈现的VC!

Crashing can also occur if the view controller cannot load from the NIB. 如果视图控制器无法从NIB加载,也会发生崩溃。 For example if there is an incorrectly named User Defined Runtime Attribute. 例如,如果存在一个错误命名的“用户定义的运行时属性”。

For example QBFlatButton changed their API today and my app started crashing. 例如, QBFlatButton今天更改了他们的API,我的应用开始崩溃。 This is why we use semantic versioning :-) 这就是为什么我们使用语义版本控制的原因:-)

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

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