Ios错误:@auto释放池“EXC_BAD_ACCESS”下的线程1

当我点击“后退”button时,应用程序崩溃并显示此错误。 我有2个视图控制器。 在第一个VC开始button工作正常切换到第二个视图,但是当我点击返回button的应用程序崩溃,我得到上面的错误在@autorelease池下面的行。 我也会发布我的开始和后退button的代码。 thx 🙂 #import #import“AppDelegate.h”

int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } 

首先vc.H文件开始button(这工作切换到第二个视图)

  @interface ViewController : UIViewController { IBOutlet UIButton *StartQuiz; IBOutlet UIButton *HowToPlay; IBOutlet UIButton *Credits; IBOutlet UIButton *Back; IBOutlet UILabel *Label; } -(IBAction)StartQuiz:(id)sender; -(IBAction)HowToPlay:(id)sender; -(IBAction)Credits:(id)sender; -(IBAction)Back:(id)sender; 

Firstvc.M文件

 @implementation ViewController -(IBAction)StartQuiz:(id)sender { Questions *MenuToQuestions = [[Questions alloc] initWithNibName:@"Questions" bundle:nil]; [self.view addSubview:MenuToQuestions.view]; } 

SecondVC.h文件(后退button崩溃的应用程序)

  @interface Questions : UIViewController { IBOutlet UIButton *BasicOptics; IBOutlet UIButton *EyeAnatomy; IBOutlet UIButton *OphthalmicInstruments; IBOutlet UIButton *Lenses; IBOutlet UIButton *Transposition; IBOutlet UIButton *Standards; IBOutlet UIButton *Frames; IBOutlet UIButton *Random; IBOutlet UIButton *Back; IBOutlet UILabel *Cat1; IBOutlet UILabel *Cat2; IBOutlet UIButton *Right1; IBOutlet UIButton *Right2; IBOutlet UIButton *Right3; IBOutlet UIButton *Right4; IBOutlet UIButton *Wrong1; IBOutlet UIButton *Wrong2; IBOutlet UIButton *Wrong3; IBOutlet UIButton *Wrong4; IBOutlet UILabel *Answer1; IBOutlet UILabel *Answer2; IBOutlet UILabel *Answer3; IBOutlet UILabel *Answer4; IBOutlet UILabel *Question; IBOutlet UILabel *SelectCategory; IBOutlet UILabel *Lives; IBOutlet UILabel *Score; IBOutlet UILabel *LivesWord; IBOutlet UILabel *ScoreWord; IBOutlet UILabel *GameOver; IBOutlet UILabel *FinalScore; } -(IBAction)BasicOptics:(id)sender; -(IBAction)EyeAnatomy:(id)sender; -(IBAction)OphthalmicInstruments:(id)sender; -(IBAction)Lenses:(id)sender; -(IBAction)Transposition:(id)sender; -(IBAction)Standards:(id)sender; -(IBAction)Frames:(id)sender; -(IBAction)Random:(id)sender; -(IBAction)Right:(id)sender; -(IBAction)Wrong:(id)sender; -(IBAction)Back:(id)sender; @end 

Secondvc.m文件

 -(IBAction)Back:(id)sender { ViewController *MenuToViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; [self.view addSubview:MenuToViewController.view]; } 

您不在任何地方持有您的MenuToViewController实例的引用。 MenuToViewController视图被添加到视图层次结构中,所以它被保留,但只要视图试图发送消息到其中一个sockets,您的应用程序就会崩溃,因为控制器已被释放。

一旦你创build了这个控制器,你可以将它设置为实例variables(将Questions *MenuToQuestions添加到你的@interface )。