Different results, same action code? (kind of)

Discussion in 'iOS Programming' started by MrM, Jan 30, 2009.

  1. MrM macrumors 6502

    Joined:
    Oct 13, 2008
    Location:
    California
    #1
    Ok guys, I'm really new to the iPhone dev scene. Anyway, I was messing around with it and I cant really understand what I am missing. I'm teaching myself how to do it, so that's probably my first error.

    Anyway, the app I'm making (for fun, not going to publish it) has 3 different buttons. I want each of those buttons to trigger a different sound. But here's my problem. Each button plays the same one. I am using IB to give each of the buttons the same action (which I've defined as "btnplaysoundClick")

    In Xcode I have the following code set up-

    PHP:
    - (IBAction)btnplaysoundClick
    {
        
    isound *sound = [[isound allocinitWithFileName:@"file1.wav"];
        [
    sound play];
        
        
    isound *soundd = [[isound allocinitWithFileName:@"file2.wav"];
        [
    soundd play];
        
        
    isound *sounddd = [[isound allocinitWithFileName:@"file3.wav"];
        [
    sounddd play];
        
    }
    I changed the file names (sound, soundd, sounddd, and file1 2 and 3 for this post.

    Anyway, does anyone see what my problem is? They all play the same sound. Any help is appreciated!
     
  2. ayasin macrumors 6502

    Joined:
    Jun 26, 2008
    #2
    Because you hooked all three buttons up to the same method. You need to create 3 methods (one with each sound) then hook each button up to it's respective method. Also you may consider releasing the isound you alloced so you don't leak memory.
     
  3. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    He could also add (id)sender as a parameter of his action method, and then examine it to see what button was pressed/which sound to play.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Like so:
    Code:
    - (IBAction)btnplaysoundClick:(id)sender
    { 
        if (sender == btn1) {
            isound *sound = [[isound alloc] initWithFileName:@"file1.wav"]; 
            [sound play];
        } else if (sender == btn2) { 
            isound *soundd = [[isound alloc] initWithFileName:@"file2.wav"]; 
            [soundd play]; 
        } else {
            isound *sounddd = [[isound alloc] initWithFileName:@"file3.wav"]; 
            [sounddd play];
        } 
         
    }  
     
  5. ayasin macrumors 6502

    Joined:
    Jun 26, 2008
    #5
    True, but based on his requirements that's more work (he'd need to create outlets for the buttons or examine some property at runtime) and less clean code wise for no real gain IMHO. Though, you are correct...it would have been more correct for me to say the easiest way to deal with this is to create 3 methods and hook each button up to a different one.
     
  6. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #6
    True...He can also use the (int)tag property of the buttons so that he doesn't have to create outlets for them. You can set their value in IB and examine it in the method to see what button was pressed.

    Or he could just load all of the sounds into an array and then play the one corresponding to the tapped button's tag:

    Code:
    - (IBAction)btnplaysoundClick:(id)sender
    { 
        UIButton *button = (UIButton *)sender;
        isound *sound = (isound *)[soundArray objectAtIndex:button.tag];
        [sound play];
    
    }
    
    This code of course assumes that the sounds were preloaded into the array elsewhere, and that the button's tags are set from 0 to 2 corresponding to how their respective sounds were put into the array.
     
  7. MrM thread starter macrumors 6502

    Joined:
    Oct 13, 2008
    Location:
    California
    #7
    Thanks for the speedy replies, guys! I will try it out and get back on here to let you know if I got it to work.
     
  8. kalimba macrumors regular

    Joined:
    Jun 10, 2008
    #8
    Once you get it working, change all of the .WAV files to be fart sounds and you'll have an instant App Store application!

    /sarcasm
     
  9. MrM thread starter macrumors 6502

    Joined:
    Oct 13, 2008
    Location:
    California
    #9
    Ha. This is purely because I want to learn and I have free time.

    Anyhow, I tried giving them all different methods (I'll try the other way next) and I made some progress. The other two buttons now have their own actions set up in IB, and are saved, but they just don't play anything now(the first one still works). Here's the code I changed in Xcode.

    Code:
    - (IBAction)btnplaysoundClick
    {
    	isound *sound = [[isound alloc] initWithFileName:@"sound.wav"];
    	[sound play];
    }
    
    
    - (IBAction)btnplaysoundClick2
    {	
    	isound *soundd = [[isound alloc] initWithFileName:@"soundd.wav"];
    	[soundd play];
    }
    
    
    - (IBAction)btnplaysoundClick3
    {
    	isound *sounddd = [[isound alloc] initWithFileName:@"sounddd.wav"];
    	[sounddd play];	
    }
    
    
    and then there is this
    Code:
    @interface iSirenViewController : UIViewController {
    
    }[B]
    - (IBAction)btnplaysoundClick; 
    - (IBAction)btnplaysoundClick2; 
    - (IBAction)btnplaysoundClick3;
    [/B]
    @end
    I feel like I'm overlooking something very simple here. Again, thanks for the help you guys!
     
  10. ayasin macrumors 6502

    Joined:
    Jun 26, 2008
    #10
    Are you sure you hooked the other buttons up to the correct methods (and removed the links to the first method)? Also release the objects you allocated after the play method, otherwise you're going to leak memory.
     
  11. MrM thread starter macrumors 6502

    Joined:
    Oct 13, 2008
    Location:
    California
    #11
    Yes, double checked and checked again. I must be missing something somewhere else. And thank you for the tip on releasing the allocated memory. I'll keep taking a stab at it and get back to you.
     
  12. ayasin macrumors 6502

    Joined:
    Jun 26, 2008
    #12
    Here's a project that I threw together that does basically what you are looking for. Rather than play a sound, it sets the background color of the view to red, green or blue depending on which button you push. You can look through it and hopefully see where you're going wrong.
     

    Attached Files:

  13. ayasin macrumors 6502

    Joined:
    Jun 26, 2008
    #13
    It has the default copyright notices that XCode throws in there (I forgot to remove them)...ignore those and feel free to do whatever you like with that project.
     

Share This Page