Set UITabBarController created in Interface Builder as delegate

后端 未结 4 1881
北荒
北荒 2021-01-05 08:33

I created my iOS app with Tab Bar template, so here is UITabBarController with bar buttons. An issue is how to set it as delegate. I found at SO that it has to be set progra

相关标签:
4条回答
  • 2021-01-05 08:35

    I don't remember exactly the Xcode's Tab Bar template set up, but in your AppDelegate you can access to your window's rootViewController, cast it to a UITabBarController, and then set its delegate to your AppDelegate or to any other view controller.

    Something like this:

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

    self.tabBarController.delegate = self;
    
    0 讨论(0)
  • 2021-01-05 08:42

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

    1. Create a new class :

      class TabBarControllerDelegate: NSObject, UITabBarControllerDelegate {

    2. In IB, add an object from the object library to the list of View Controller on the left (note: search "object", it's a yellow cube).

    3. Change the class of this object (your yellow cube in IB) to TabBarControllerDelegate

    4. In IB navigate to your Tab Bar Controller Scene. From the Connection Inspector, drag the delegate circle to the new object you added in Step 3.

    5. Implement your delegate methods in your TabBarControllerDelegate class. Done!

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

      }

    0 讨论(0)
  • 2021-01-05 08:57

    What about create a viewController lets say MyTabController subclass UITabBarController

    @interface MyTabController : UITabBarController<UITabBarControllerDelegate>
    

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

    implement:

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

    and here you are.

    Edit:

    If you find self.delegate = self; is odd, which it is, you can create an outlet in your MyTabController

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

    Then you can put tabBarController.delegate = self;

    0 讨论(0)
  • 2021-01-05 09:00

    0 lines of code

    Drag an Object and subclass it

    1. Xcode > Show File Inspector > Custom Class.
    2. Class: TabBarControllerDelegate.

    Set delegate to that Object


    Put your existing code in that Object

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

    class TabBarControllerDelegate: NSObject, UITabBarControllerDelegate {
        // Delegate code goes here
    }
    
    0 讨论(0)
提交回复
热议问题