Set UITabBarController created in Interface Builder as a delegate

I created an iOS application with a tab bar template, so here is the UITabBarController with buttons on the bar. The problem is how to set it up as a delegate. I found in SO that it should be installed programmatically in AppDelegate, but I believe that this is not possible because I do not have access to the tab bar controller as an output.

I added the correct value to @interface (both ViewController and AppDelegate), but don't know what to do next.

 @interface ViewController : UIViewController <UITabBarControllerDelegate> 

I hope I do not need to get rid of the entire application template, and you can configure the Tab Bar Controller created in IB for delegation.

I definitely want him to delegate to create a tab selection in the event, like this:

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

Any idea?

+6
source share
4 answers

I don’t remember the Xcode Tab tab template template, but in AppDelegate you can access your rootViewController window, drop it to the UITabBarController and then set its delegate to AppDelegate or to any other view controller.

Something like that:

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

EDIT

If you want to install an instance of ViewController as a delegate:

 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 From the ViewController itself, you can set the tab bar controller delegate as rdelmar comments. Just keep in mind that this cannot be done in the init method, because the view controller is not yet in the tab bar controller. The correct place will be viewDidLoad , but therefore it will not be executed until the ViewController is loaded ...

 self.tabBarController.delegate = self; 
+5
source

You can quickly and easily create a new TabBarController delegate class and associate it as a delegate in a storyboard.

  • Create a new class:

    class TabBarControllerDelegate: NSObject, UITabBarControllerDelegate {

  • In IB, add an object from the object library to the list of the view controller on the left (note: the search for "object" is a yellow cube).

  • Change the class of this object (yellow cube in IB) to TabBarControllerDelegate

  • In IB mode, go to the tab bar controller screen. From the Connection Inspector, drag the delegate circle to the new object you added in step 3.

  • Embed your delegate methods in your TabBarControllerDelegate class. Done!

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

    }

+8
source

How about creating a viewController lets say MyTabController subclass of UITabBarController

 @interface MyTabController : UITabBarController<UITabBarControllerDelegate> 

and set the Controller tab class for the storyboard to MyTabController instead of UITabBarController , then put self.delegate = self; in viewDidLoad

implementation of

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

and here you are.

Edit:

If you find that self.delegate = self; is odd, that is, you can create an output in MyTabController

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

Then you can put tabBarController.delegate = self;

+3
source

0 lines of code

Drag object and subclass

IB Object

  • Xcode> Show File Inspectors> Custom Class.
  • Class : TabBarControllerDelegate .

Set a delegate for this object

Object reference in IB


Put existing code in this object

This is the code that you already have in the current UITabBarControllerDelegate .

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

Source: https://habr.com/ru/post/951500/


All Articles