0

我正在嘗試將一個信息按鈕添加到導航欄的頂部。信息按鈕將用戶引導至信息視圖。以下代碼將iPhone默認信息按鈕添加到導航欄。它被放置在RootViewController.m的viewDidLoad函數中。現在如何將信息圖標添加到導航欄並實現showInfoView方法?

UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    [infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside]; 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton]; 

,該showInfoView方法需要實現,並使用下面的代碼:

- (IBAction)showInfoView:(id)sender { 

// Create the root view controller for the navigation controller 
// The new view controller configures a Cancel and Done button for the 
// navigation bar. 
InfoViewController *addController = [[InfoViewController alloc] 
              initWithNibName:@"InfoView" bundle:nil]; 

// Configure the RecipeAddViewController. In this case, it reports any 
// changes to a custom delegate object. 
addController.delegate = self; 

// Create the navigation controller and present it modally. 

UINavigationController *navigationController = [[UINavigationController alloc] 
               initWithRootViewController:addController]; 
[self presentModalViewController:navigationController animated:YES]; 

// The navigation controller is now owned by the current view controller 
// and the root view controller is owned by the navigation controller, 
// so both objects should be released to prevent over-retention. 
[navigationController release]; 

[addController release];  

}

但是,當該程序生成並運行,點擊信息按鈕結果程序崩潰。

以前有沒有人遇到過這個問題?我已經廣泛搜索,並且我沒有看到有任何執行showInfoView方法時遇到麻煩。

回答

-1

使用此代碼:

UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside]; 
self.navigationItem.rightBarButtonItem = infoButton; 

應該正常工作提供showInfoView:方法筆尖文件名是「InfoView.xib」而不是「InfoViewController.xib」

+0

拋出以下錯誤:***終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [UIButton createViewForNavigationItem:]:無法識別的選擇器發送到實例0x4b51bb0' – Richard 2012-03-24 20:57:43

相關問題