2012-03-26 146 views
-1

我已經加入導航控制器到我器件的應用在didFinishLaunchingWithOptions這樣navigationcontroller導致應用程序崩潰

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刪除導航控制器它的工作原理...Ÿ我不能讓我的導航正常工作,我怎麼能避免它墜毀。

+0

請添加您在控制檯中出現錯誤。 – hchouhan02 2012-03-26 06:50:39

回答

2

修改代碼

試試這個代碼 聲明appdelete.h文件

@class LoginViewController; 

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

的appdelegate。M檔在didFinishLaunchingWithOptions聲明

@synthesize viewController; 

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

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

它運行在iPhone模擬器第一次當我運行的應用程序。在手機上它崩潰.. – sansid1983 2012-03-26 06:40:27

+0

好吧,你可以嘗試這種方法 – akk 2012-03-26 06:47:11

+0

看到我的更新回答 – akk 2012-03-26 06:53:46

0

我這個這導致死機。

[phButton release]; 

phButton不需要發佈;

順便說一句,

phBarButtonItem需要被釋放;

0

我認爲問題出在您的導航控制器autorelease。

  1. 你嘗試刪除autorelease

navigationController = [[UINavigationController的 的alloc] initWithRootViewController:MAINVIEW];

  1. 嘗試使用屬性(如果有的話),

self.navigationController = [[[UINavigationController的 的alloc] initWithRootViewController:MAINVIEW]自動釋放];

+0

試過這個..沒有工作 – sansid1983 2012-03-26 06:26:53

+0

@ user1168699。您從您的viewview的主視圖中刪除所有代碼。主視圖中可能會有一些問題導致問題出現。 – Vignesh 2012-03-26 06:33:48

+0

發現當我試圖設置導航欄的背景圖像時,它崩潰.. – sansid1983 2012-03-26 06:55:24

0

試試這個...

self.window.rootViewController = navigationController; 

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

+0

我試過這個......但它仍然崩潰 – sansid1983 2012-03-26 06:32:59

+0

@ user1168699:我認爲這必須正常工作,因爲我已經實現了相同的概念對我來說它工作正常....檢查你的initWithNibName:@「」...你給與它相同的.xib名? – Krunal 2012-03-26 06:36:15

+0

.xib是正確的...每當我在模擬器上運行應用程序時,我必須重置。那麼只有它的作品......但在手機上它根本不工作...當我刪除導航欄,然後它工作正常 – sansid1983 2012-03-26 06:42:40

0

[phButton發佈];刪除該行&

IMG & phBarButtonItem需要被釋放

+0

釋放他們...沒有任何反應 – sansid1983 2012-03-26 06:33:27

+0

在viewDidLoad函數中設置斷點。找到應用程序正確崩潰或啓用NSZombie跟蹤崩潰 – Rams 2012-03-26 06:48:18

+0

發現,當我試圖設置導航的背景圖像吧它崩潰.. – sansid1983 2012-03-26 06:56:20