0

当我点击返回按钮时,应用程序崩溃并显示此错误。我有 2 个视图控制器。在第一个 vc 上,“开始”按钮可以很好地切换到第二个视图,但是当我点击“后退”按钮时,应用程序崩溃了,并且我在 @autorelease 池下面的行中得到了上面的错误。我还将发布我的开始和后退按钮的代码。thx :) #import #import "AppDelegate.h"

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

第一个 vc.H 文件开始按钮(这可以切换到第二个视图)

 @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文件(后退按钮使应用程序崩溃)

 @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];

}
4

1 回答 1

1

您没有在任何地方持有您的MenuToViewController实例的引用。视图MenuToViewController被添加到视图层次结构中,因此它被保留,但是一旦视图尝试向其中一个出口发送消息,您的应用程序就会崩溃,因为控制器已被释放。

创建该控制器后,您可以将其设置为实例变量(添加Questions *MenuToQuestions到您的@interface)。

于 2013-06-25T12:20:45.133 回答