navigationcontroller导致应用崩溃

时间:2012-03-26 06:13:47

标签: iphone xcode uinavigationcontroller

我在这样的didFinishLaunchingWithOptions中添加了导航控制器到我的应用中

LoginViewController *mainView = [[[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil] autorelease];
navigationController = [[[UINavigationController alloc]initWithRootViewController:mainView]autorelease];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];

在我的LoginViewController的viewDidLoad中,

self.navigationItem.hidesBackButton = YES;
[super viewDidLoad];

UIImage *img = [[UIImage alloc] init];
img = [UIImage imageNamed:@"top_bar.png"];
bar = [self.navigationController navigationBar];
[bar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];

UIImage *signIn = [UIImage imageNamed:@"signin_btn.png"];
UIButton *phButton = [UIButton buttonWithType:UIButtonTypeCustom];
[phButton setImage:signIn forState:UIControlStateNormal];
phButton.frame = CGRectMake(0.0, 0.0, signIn.size.width, signIn.size.height);
UIBarButtonItem *phBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:phButton];
self.navigationItem.rightBarButtonItem = phBarButtonItem;
[phButton addTarget:self action:@selector(checkConnection) forControlEvents:UIControlEventTouchUpInside];
[phButton release];

当我在手机中运行该应用程序时,它会崩溃。当我从appDelagate中删除导航控制器时,它可以工作......我无法让我的导航正常工作,我怎样才能避免它崩溃。

5 个答案:

答案 0 :(得分:2)

修改您的代码

试试这段代码 声明appdelegate.h文件

@class LoginViewController;  

LoginViewController *viewController; 
@property (nonatomic, retain) IBOutlet LoginViewController *viewController;

appdelegate.m文件声明

@synthesize viewController; 

在didFinishLaunchingWithOptions

UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:nav.view];
[window makeKeyAndVisible];
return YES;

  -(void) dealloc
 {
 [viewController release]; 
 ......//some code
 }

答案 1 :(得分:0)

我这导致崩溃。

[phButton release];

phButton不需要发布;

顺便说一下,

需要释放

phBarButtonItem;

答案 2 :(得分:0)

我认为问题在于您使用导航控制器进行自动释放。

  1. 您尝试删除autorelease
  2.   

    navigationController = [[UINavigationController   页头] initWithRootViewController:MAINVIEW];

    1. 尝试使用属性(如果有),
    2.   

      self.navigationController = [[[UINavigationController   页头] initWithRootViewController:MAINVIEW]自动释放];

答案 3 :(得分:0)

试试这个...

self.window.rootViewController = navigationController;

而不是[self.window addSubview:navigationController.view];

答案 4 :(得分:0)

[phButton发布];删除该行&

img & phBarButtonItem 需要发布