Ios error: Thread 1 under @auto release pool "EXC_BAD_ACCESS

我怕爱的太早我们不能终老 提交于 2019-12-11 19:22:16

问题


The app crashes and shows this error when I hit the Back button. I have 2 view controllers. On the first vc the Start button works fine to switch to second view, but when I hit the Back button the app crashes and I get the error above on the line below @autorelease pool. I will also post the code for my start & back buttons. thx :) #import #import "AppDelegate.h"

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

First vc.H file Start Button (this works switching to second view)

 @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 file

@implementation ViewController

-(IBAction)StartQuiz:(id)sender {
Questions *MenuToQuestions = [[Questions alloc]
                              initWithNibName:@"Questions"
                              bundle:nil];

[self.view addSubview:MenuToQuestions.view];

}

SecondVC.h file (Back button crashes app)

 @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 file

-(IBAction)Back:(id)sender {

ViewController *MenuToViewController = [[ViewController alloc]
                              initWithNibName:@"ViewController"
                              bundle:nil];

[self.view addSubview:MenuToViewController.view];

}

回答1:


You are not holding a reference for your MenuToViewController instance anywhere. The view of the MenuToViewController gets added to the view hierarchy, so it gets retained, but as soon as the view tries to send a message to one of the outlets, your app crashes, because the controller has been released.

Once you create that controller, you could set it as instance variable (add Questions *MenuToQuestions to your @interface).



来源:https://stackoverflow.com/questions/17297034/ios-error-thread-1-under-auto-release-pool-exc-bad-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!