2013-08-11 10 views
6

タブバーテンプレートを使用してiOSアプリケーションを作成しました。ここでは、バーボタン付きのUITabBarControllerがあります。問題はデリゲートとして設定する方法です。私はそれがAppDelegateでプログラムで設定されなければならないことがわかったが、私はそれが不可能だと信じている。なぜなら、アウトレットとしてTab Bar Controllerへのアクセスがないからだ。デリゲートとしてInterface Builderで作成されたUITabBarControllerを設定します

@interface(ViewControllerとAppDelegateの両方)に適切な値を追加しましたが、次に何をするか分かりません。

@interface ViewController : UIViewController <UITabBarControllerDelegate> 

私は、全体のアプリテンプレートを取り除くために持っていないことを望むと、それはタブバーコントローラはデリゲートであることをIBで作成し設定することが可能です。

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; 

任意のアイデアを:

まさに私はそれがこのようなタブを選択したイベントに作成するために委任するようにしたいですか?

答えて

5

を置くことができ、私は「ドンXcodeのタブバーのテンプレート設定を正確に覚えておいてください。ただし、AppDelegateではウィンドウのrootViewControllerにアクセスしてUITabBarControllerにキャストしてくださいデリゲートをAppDelegateまたは他のView Controllerに設定します。

このような何か:

UITabBarController *tabBarController = 
    (UITabBarController *)[[self window] rootViewController]; 
[tabBarController setDelegate:self]; // In this example your app delegate would implement the UITabBarControllerDelegate protocol. 

EDIT

をデリゲートとしてあなたのViewControllerのインスタンスを設定する場合:

UITabBarController *tabBarController = 
     (UITabBarController *)[[self window] rootViewController]; 
// I assume you have your ViewController instance set as the first view controller of your tab bar controller 
// No need for a cast here since objectAtIndex: returns id, but of course you must implement the UITabBarController protocol in your ViewController. 
    [tabBarController setDelegate:[[tabBarController viewControllers] objectAtIndex:0]]]; 

EDIT 2 あなたのViewController自体から、あなたが設定することができますrdelmarコメントとしてのタブバーコントローラの代理人。 ビューコントローラがタブバーコントローラにまだないので、これはinitメソッドでは実行できないことに注意してください。適切な場所はviewDidLoadですが、ViewControllerビューが読み込まれるまで実行されません。

self.tabBarController.delegate = self; 
+0

これを試しましたが、うまくいかないようです。私はこの方法が正しいと信じています(エラーや警告はありません)が、タブが変更されたときに 'tabBarController:didSelectViewController'は起動しません。 –

+0

私のせいに気づいた。それは起動しますが、AppDelegate.mに配置する必要があります。したがって、ViewController.mの内部で動作させる方法はありませんか? –

+0

tabBarControllerを実装している場所:didSelectViewController :?あなたのアプリケーションデリゲートでは、そうですか?デバッグし、tabBarControllerがnilでないことを確認できますか? – e1985

3

何についてviewControllerを作成すると、実装UITabBarController

@interface MyTabController : UITabBarController<UITabBarControllerDelegate> 

MyTabControllerサブクラスを言うと、あなたの中にMyTabController代わりのUITabBarControllerからstoryboardをタブコントローラのクラスを設定し、その後、あなたのviewDidLoad

self.delegate = self;を置くことができます:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; 

ここにいるのです。

編集:

あなたはself.delegate = self;を見つけた場合、あなたのMyTabController

IBOutlet UITabBarController *tabBarController; and connect it to the tab controller in your storyboard. 

でアウトレットを作成することができ、それがある、奇数であるその後、tabBarController.delegate = self;

+1

このケースではUITabBarControllerサブクラスを作成する必要はなく、独自のデリゲートであるという考えは嫌いです。 – e1985

+3

これは、appDelegateにコードを追加するよりも優れています。あなたはVCをより良くコントロールできます。これをチェックしてくださいhttp://stackoverflow.com/questions/11065605/didselectviewcontroller-method-not-being-called-with-storyboard/13033372#13033372 – null

+0

私が言うように、私はそれが良いデザインのUITabBarControllerサブクラスを持っているとは思わないあなたのVCをより良くコントロールする方法を知ることはできません。独自のデリゲートを実装するオブジェクトについては、この投稿http:// stackoverflowをチェックしてください。com/questions/13310287/can-an-objective-c-object-be-own-delegate-it-good-programming-practice – e1985

7

新しいTabBarController Delegateクラスをすばやく簡単に作成し、ストリーミングボードのデリゲートとして接続することができます。

  1. 新しいクラスを作成します。

    クラスTabBarControllerDelegate:NSObjectの、UITabBarControllerDelegateを{IBでは

  2. 、左側のオブジェクトライブラリからのビューコントローラのリストにオブジェクトを追加します(注意:検索 "オブジェクト"、それは黄色のキューブです)。

  3. 変更IBで

  4. をTabBarControllerDelegateするには、このオブジェクト(IBであなたの黄色の立方体)のクラスは、あなたのタブバーコントローラのシーンに移動します。接続インスペクタから、デリゲートサークルを手順3で追加した新しいオブジェクトにドラッグします。

  5. TabBarControllerDelegateクラスでデリゲートメソッドを実装します。完了!コードの

    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController)->Bool { 
        println("Selected a new tab") 
    

    }

+0

遠くから、コードを含まないソリューションが最適なソリューションです。 – SwiftArchitect

3

0行

ドラッグオブジェクトおよびサブクラスそれ

IB Object

  1. のXcode>表示ファイルを調べまたは>カスタムクラス。
  2. クラスTabBarControllerDelegate

そのオブジェクトに設定デリゲート

Link to object in IB


これはあなたが既に持っているコードしているオブジェクトに

を既存のコードを入れてください現在のUITabBarControllerDelegate

class TabBarControllerDelegate: NSObject, UITabBarControllerDelegate { 
    // Delegate code goes here 
} 
+0

ありがとうございます。これは私のために働く。 –