-3

I have declared a UITabBarController in my AppDelegate.m file. I have two viewcontrollers in this UITabBarController.

In each of the views, lets call them view1 and view2, i have the following code:

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self)
    {
        UITabBarItem *tbi = self.tabBarItem;
        tbi.title = @"title";
        UIImage *i = [UIImage imageNamed:@"picture.png"];
        tbi.image = i;
        tbi.tag = 0;
    }

This makes me able to freely navigate between two viewcontrollers.

Now, to the question:

I want to have an event when i click each of the tabs. I have googled for 4 hours now and tried a lot of things. I'm close to going insane.

Where do i put what kind of code to be able to execute something when i click each tab?

Please dont say " you need to implement UITabBarDelegate and then use -void(didselectitem) " without explaining exactly how :)

EDIT: My AppDelegate.h file:

#import <UIKit/UIKit.h>

@interface PETTAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate, UITabBarDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

My AppDelegate.m file:

@implementation PETTAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
NSBundle *appBundle = [NSBundle mainBundle];


PETTSoundBoardPage *sbP = [[PETTSoundBoardPage alloc]initWithNibName:@"PETTSoundBoardPage" bundle:appBundle];
PETTNPage * nP = [[PETTNPage alloc]initWithNibName:@"PETTNPage" bundle:appBundle];

UITabBarController *tBC = [[UITabBarController alloc]init];
tBC.tabBarController.delegate = self;
tBC.viewControllers = @[sbP,nP];

self.window.rootViewController = tBC;


self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;

}

sbP and nP file (almost identical code except for title and picture):

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
    UITabBarItem *tbi = self.tabBarItem;
    tbi.title = @"Soundboard";
    UIImage *i = [UIImage imageNamed:@"picture.png"];
    tbi.image = i;
    tbi.tag = 0;
Pett
  • 3
  • 4
  • What exactly are you trying to achieve with your event? – Nate Lee May 17 '14 at 18:39
  • @nlee918 I'm trying to play a little click-like sound when they press each tab, from a mp3 file i have. But to do that i first need to find some area of code that will run every time the user clicks each tab – Pett May 17 '14 at 18:40

1 Answers1

0

Use the UITabBarControllerDelegate method, didSelectViewController:. I'm assuming that you have already prepared the sound playing method, so implement it as such in your AppDelegate.

Of course, you have to add the UITabBarControllerDelegate to your interface as well by doing this:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

Add this code to your .m file of your app delegate.

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

//Play your sound here

}

Info here

EDIT: You need to set your tabBarController's delegate to yourself. To do that do this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    //get reference to your view controller,

    myViewController.tabBarController.delegate = self;
    return YES;
}
Nate Lee
  • 2,842
  • 1
  • 24
  • 30
  • Thanks a lot for answering! However, i dont know where or how to implement the delegate, since my viewcontroller is a XIB file (interface builder) Should i implement the method in the AppDelegate, or in each of the viewcontroller classes? – Pett May 17 '14 at 18:46
  • Maybe I didn't make it clear enough. This is in your app delegate. – Nate Lee May 17 '14 at 18:46
  • @interface AppDelegate : UIResponder – Nate Lee May 17 '14 at 18:47
  • Thats also in your delegate – Nate Lee May 17 '14 at 18:51
  • thanks for the quick responses! -However, i still cant get it working. i now have the @interface UITabBarDelegate in the AppDelegate.h file, and the code you posted inn the AppDelegate.m file. To test, i just wrote NSLog(@"123"); inside the method, but the string doesnt appear in console, so it doesnt respond when i click each tab :/ – Pett May 17 '14 at 18:55
  • Sorry, I forgot to tell you to set the delegate to self. Check my edit. – Nate Lee May 17 '14 at 19:00
  • Still doesn't work, i edited and added a lot of info – Pett May 17 '14 at 19:10
  • Is that all you have in your App delegate? – Nate Lee May 17 '14 at 19:12
  • the .h file is all i have yes. i also have the method you posted – Pett May 17 '14 at 19:13
  • Change tBC.tabBarController.delegate = self; to tBC.delegate = self; – Nate Lee May 17 '14 at 19:16
  • Finally! After 5 hours of trying to figure all this out! Worked now, thanks a lot! :) – Pett May 17 '14 at 19:19