2016-10-17 9 views
0

UITabBarDelegateをサブクラスUIViewControllerに設定しようとすると、アプリがクラッシュする問題が発生します。私はUITabBarViewControllerいくつかのTabItemsビューコントローラにリンクしている。これらのView Controllerの1つはHomeViewControllerです。私はHomeViewControllerに次のコードを持っている:UITabBarDelegateをUIViewControllerに設定するとアプリケーションがクラッシュする

class HomeViewController: UIViewController, UITabBarDelegate { 
     override func viewDidLoad() { 
     super.viewDidLoad() 
     self.tabBarController?.tabBar.translucent = false 
     self.extendedLayoutIncludesOpaqueBars = true 
     self.tabBarController?.tabBar.delegate = self //This is causing crash 
    } 
} 

私が削除した場合self.tabBarController?.tabBar.delegate = selfラインすべてが正常に動作し、期待通りの私のタブバーの動作が、私はときに私は、次のクラッシュを取得し、その行を再度追加:

ibc++abi.dylib: terminating with uncaught exception of type NSException 

これを解決する方法が完全にはわかりません。 SOには他にもいくつかの回答がありましたが、このプロセスをどのようにするかはまだ不明です。

ありがとうございます!

+0

キャッチ例外と ' そして、それは –

+0

あなたUITabBarViewControllerがすでにUITabBarにデリゲートで言っていることを示してください。この '印刷(exception.localizedDescription)を使用して例外の説明を印刷。デリゲートを別のviewControllerに設定する必要があるのはなぜですか? –

+0

私は、HomeButton項目 "Home"がタッチされたときに実行したいHomeViewController内にメソッドを持っています。これを行うより良い方法はありますか? –

答えて

1

あなたのUITabBarViewControllerはすでにあなたのUITabBarの代理人です。 viewControllerをtabBarDelegateにする代わりに、tabBarViewControllerを使用してロジックを配置します。

このようなことができます。 tabBarViewControllerのdidSelectItemのデリゲートメソッドで

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) 
{ 
    let index = tabBar.items?.indexOf(item) 

    if (index == /*the required index of HomeViewController*/) 
    { 
     let homeVC = self.viewControllers.objectAtIndex(index) as! HomeViewController 
     homeVC.myMethod() 
    } 

} 
+0

うん、これはうまくいきました。ありがとう助けをたくさん! –

+0

助けてくれてうれしい –

関連する問題