2

我正在尝试创建一个后退按钮以返回我的应用程序的主菜单,现在发生的事情是,一旦我按下后退按钮,它就会显示主菜单,但是应用程序在一秒钟后崩溃了。

我用于后退按钮的代码是;

- (void)backButtonPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

从 AppDelegate.m 文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[MenuViewController alloc] initWithNibName:@"MenuViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.viewController = [[[MenuViewController alloc] initWithNibName:@"MenuViewController_iPad" bundle:nil] autorelease];
    }
    navigationController = [[myNavigationViewController alloc] initWithRootViewController:self.viewController];
    navigationController.navigationBarHidden = YES;
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

我看不到哪里出错了?它似乎调用了正确的屏幕,但之后立即崩溃?

编辑 - 这是 MenuViewController.m 文件;

#import "MenuViewController.h"
#import "MagicAppDelegate.h"
#import "MagicViewController.h"

@interface MenuViewController ()

@end

@implementation MenuViewController

- (void)viewDidLoad
{
    self.view.frame = [[UIScreen mainScreen] bounds];


    [super viewDidLoad];

  //  [self initLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

-(IBAction)onStart:(id)sender
{
    MagicViewController* viewController;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        viewController = [[MagicViewController alloc] initWithNibName:@"MagicViewController_iPhone" bundle:[NSBundle mainBundle]];
    else
        viewController = [[MagicViewController alloc] initWithNibName:@"MagicViewController_iPad" bundle:[NSBundle mainBundle]];

    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
}

- (void)dealloc {

    [super dealloc];
}

@end
4

2 回答 2

0

请使用下面的代码,你想去 mainmenuview 控制器吗?

 [self.navigationController popToRootViewControllerAnimated:YES];
于 2013-04-03T05:08:17.620 回答
0

实际上,我不明白您的代码:在语句navigationController = [[myNavigationViewController alloc] initWithRootViewController:self.viewController];中,您发送的alloc东西似乎是class的实例UINavigationController,但 alloc 是一个方法。因此,我假设 myNavigationViewController 是 UINavigationController 的子类(但应该以大写字母开头)。
然后将返回的新实例直接分配给变量,即不使用 setter 方法navigationController。因此保留。如果您的语句返回一个 autorelease 对象,那么一旦程序返回到主事件循环,它就会被释放。
因此尝试使用setter方法,即self.navigationController = [[myNavigationViewController alloc] initWithRootViewController:self.viewController];

于 2013-04-03T06:02:44.717 回答