2

アプリのメイン メニューに戻るための戻るボタンを作成しようとしています。現在何が起こっているかというと、戻るボタンを押すとメイン メニューが表示されますが、1 秒後にアプリがクラッシュします。

戻るボタンに使用するコードは次のとおりです。

- (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

以下のコードを使用してください。メインメニュー ビュー コントローラーに移動しますか。

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

実際、私はあなたのコードを理解していません:ステートメントでは、 classのインスタンスのように見えるものに をnavigationController = [[myNavigationViewController alloc] initWithRootViewController:self.viewController];送信していますが、 alloc はクラスメソッドです。したがって、myNavigationViewController は UINavigationController のサブクラスであると想定しています (ただし、大文字で始める必要があります)。 返された新しいインスタンスは直接、つまり setter メソッドを使用せずに variable に割り当てられます。したがって、保持されません。ステートメントが autorelease オブジェクトを返す場合、プログラムがメイン イベント ループに戻るとすぐに解放されます。 したがって、setter メソッドを使用してみてください。つまり、allocUINavigationController
navigationController
self.navigationController = [[myNavigationViewController alloc] initWithRootViewController:self.viewController];

于 2013-04-03T06:02:44.717 に答える