2015-03-02 116 views
0

我想在swift項目中使用「AHTabBarController」。AHTabBarController與Swift不兼容

我不知道爲什麼這個代碼不工作:

 var tabBarController = self.window?.rootViewController as AHTabBarController 

     var tab1 = AHTabView() 
     tab1.image = UIImage(named: "") 
     tab1.title = "Tab 1" 

     var tab1_subItem = AHSubitemView(frame: UIScreen.mainScreen().bounds) 
     tab1_subItem.image = UIImage(named: "") 
     tab1_subItem.title = "Tab 1 Title" 
     tab1_subItem.viewControllerIdentifier = "channelsListSB" 

     tab1.addSubitem(tab1_subItem) 


     tabBarController.tabs.addObjectsFromArray([tab1]) 

     self.window?.rootViewController = tabBarController 

Objective-C的代碼是:

AHTabBarController *tabBarController = (AHTabBarController*)self.window.rootViewController; 

/******* PURUS *******/ 
AHTabView *purus = [AHTabView new]; 
[purus setImage:[UIImage imageNamed:@"persondot"]]; 
[purus setTitle:@"Purus"]; 

AHSubitemView *pFirst = [AHSubitemView new]; 
[pFirst setImage:[UIImage imageNamed:@"persondot"]]; 
[pFirst setTitle:@"First"]; 
[pFirst setViewControllerIdentifier:@"PurusFirst"]; 
[purus addSubitem:pFirst]; 

// … 

AHSubitemView *fFirst = [AHSubitemView new]; 
[fFirst setImage:[UIImage imageNamed:@"photos"]]; 
[fFirst setTitle:@"First"]; 
[fFirst setViewControllerIdentifier:@"FringillaFirst"]; 
[fringilla addSubitem:fFirst]; 
//Don't forget to add your AHTabView instances to the AHTabBarController! 
[tabBarController.tabs addObjectsFromArray:@[purus, fringilla, ipsum]]; 

的錯誤是:

fatal error: unexpectedly found nil while unwrapping an Optional value

+1

*我不知道爲什麼這段代碼不工作*是一個完全無用的描述。什麼不能像例外一樣工作?你能指望什麼? – idmean 2015-03-02 16:37:13

+0

致命錯誤:在解包可選值時意外發現爲零 – Ahmad 2015-03-02 18:23:53

+0

您是否知道打開可選值的含義?哪一行導致此問題? – 2015-03-02 18:29:11

回答

1

你不要指定一條線,所以我只是猜測問題在這裏:

tab1.image = UIImage(named: "") 

這是返回nil,但您將它分配給image,這是一個隱式解包可選。您需要指定一個確保返回圖像的實際圖像名稱。或者,你可以有條件地這樣做:

if let image = UIImage(named: "") { 
    tab1.image = image 
} else { 
    // handle failure 
} 
+0

2015-03-02 22:22:58.491 altelfaz [25005:2046895] CUICatalog:提供的資產名稱無效: [25005:2046895] ***終止應用程序由於未捕獲的異常'無效的ViewController!',原因:'ViewController實例化的故事板可能不是零。請檢查標識符是否有效' – Ahmad 2015-03-02 21:23:24