Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7826677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:31:32+00:00 2026-06-02T09:31:32+00:00

I would like to associate a sound effect when pressing down a uibutton. So

  • 0

I would like to associate a sound effect when pressing down a uibutton. So far i have associate a method to the touch down event

 [allButton addTarget:self action:@selector(showAll) forControlEvents:UIControlEventTouchDownInside];

and call the play sound method within the method called:

 - (void)showAll
{
    [self.buttonSoundEffect play];

    ...
}

Is there a better way to do this? Can I subclass the UIButton class to handle the sound effect and refer to this new UIButton class for each button specific to my app?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:31:42+00:00Added an answer on June 2, 2026 at 9:31 am

    I believe creating a category is the way to go.

    I proceed this way:

    .h:

    #import <UIKit/UIKit.h>
    
    @class SCLSoundEffect;
    
    typedef enum {
      SCLCLICKSOUND = 0,
      SCLOTHERSOUND,  
    } SCLSoundCategory;
    
    
    @interface UIButton (soundEffect)
    
    @property (nonatomic, strong) SCLSoundEffect *buttonSoundEffect;
    
    
    + (id) buttonWithType:(UIButtonType)buttonType andSound: (SCLSoundCategory)soundCategory;
    - (void) playSound;
    
    @end
    

    .m:

    #import "UIButton+soundEffect.h"
    #import <objc/runtime.h>
    #import "SCLSoundEffect.h"
    
    static char const * const kButtonSoundEffectKey = "buttonSoundEffect";
    
    @implementation UIButton (soundEffect)
    
    @dynamic buttonSoundEffect;
    
    
    + (id) buttonWithType:(UIButtonType)buttonType andSound:(SCLSoundCategory) soundCategory;
    {
        UIButton *newButton = [UIButton buttonWithType:buttonType];
    
        NSString *stringToUse = nil;
    
        switch (soundCategory) {
            case SCLCLICKSOUND:
                stringToUse = @"button_sound.wav";
                break;
            case SCLOTHERSOUND:
                assert(0); // To be defined
    
            default:
                break;
        }
    
        [newButton setButtonSoundEffect: [[SCLSoundEffect alloc] initWithSoundNamed:stringToUse]];
        [newButton addTarget:newButton action:@selector(playSound) forControlEvents:UIControlEventTouchDown];
    
        return newButton;
    }
    
    
    - (void) playSound
    {
        [self.buttonSoundEffect play];
    }
    
    
    - (SCLSoundEffect *)buttonSoundEffect {
        return objc_getAssociatedObject(self, kButtonSoundEffectKey);
    }
    
    - (void)setButtonSoundEffect:(SCLSoundEffect *)buttonSoundEffect{
        objc_setAssociatedObject(self, kButtonSoundEffectKey, buttonSoundEffect, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (void) dealloc
    {
        [self setButtonSoundEffect:nil];
    }
    

    Now each time I create a button that play some sound I just need to use the following method:

    UIButton *mySoundButton = [UIButton buttonWithType:UIButtonTypeCustom andSound:SCLCLICKSOUND];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tree panel and I would like to associate an action to
I have 20 digits and I would like to associate them with strings. Is
I have created a JFrame and I would like to associate a specific behaviour
I'm using Flot and would like to associate text with each individual plot point
i have just started learning linq because i like the sound of it. and
I have a DataGrid with info like this: Associate Amount Time Customer John Doe
I would like to associate the last installed version of my Mac OS X
I would like to associate a specific file type with my application, so when
I would like to be able to associate various models (Venues, places, landmarks) with
I would like to create a custom error domain for my application. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.