首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS AppDelegate 中載入 ViewController 發生 nib 未連結錯誤

     編輯:程式設計綜合問答

錯誤訊息

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "CalendarView" nib but the view outlet was not set.'

原因分析

CalendarViewController 關聯的 XIB/Storyboard 中,**File's Owner 的 view outlet 未連結到視圖物件**。當控制器嘗試載入 nib 時,找不到根視圖,拋出異常。

解決步驟

  1. 開啟 CalendarView.xib(或 Storyboard 中對應場景)。
  2. 選取 **File's Owner**,Identity Inspector 確認 Custom Class 設為 CalendarViewController。
  3. 右鍵點擊 File's Owner,找到 view outlet,拖曳連線到畫面上的根 View 物件。
  4. 若使用 Storyboard,確認場景的 View Controller Class 正確,且根視圖已自動連結。

程式碼檢視

原始碼中以 CalendarViewController *calendar = [[CalendarViewController alloc] init]; 初始化,若有同名 nib 會自動載入。若 nib 名稱與類別不同,需改用 initWithNibName:bundle: 指定。

補充建議

  • 現代 iOS 開發建議改用 **Storyboard** 或 **程式碼佈局**,減少 XIB 管理負擔。
  • 若在 AppDelegate 中建立根視圖控制器,確保 window.rootViewController 指派正確,並在 didFinishLaunchingWithOptions 中完成。
  • 檢查 Target → General → Main Interface 設定是否正確。

修正 outlet 連結後,重新執行即可解決「view outlet was not set」崩潰問題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved