我正在尝试创建一个后退按钮以返回我的应用程序的主菜单,现在发生了什么,是,一旦我按下后退按钮,它会显示主菜单,但随后应用程序会在一秒后崩溃
我用于后退按钮的代码是;
- (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
答案 0 :(得分:0)
请使用下面的代码,你想去mainmenuview控制器吗?
[self.navigationController popToRootViewControllerAnimated:YES];
答案 1 :(得分:0)
实际上,我并不理解您的代码:在navigationController = [[myNavigationViewController alloc] initWithRootViewController:self.viewController];
语句中,您发送的alloc
内容似乎是类{的实例 {1}},但alloc是类方法。因此我假设myNavigationViewController是UINavigationController的子类(但是应该以大写字母开头)。
然后,返回的新实例直接分配给变量UINavigationController
,即不使用setter方法。因此不保留。如果你的语句返回一个autorelease对象,它将在程序返回主事件循环后立即释放
因此,尝试使用setter方法,即navigationController