views:

196

answers:

4

A... as I learn Objective C.

There will be close to 20 button on a view at a time... :)

If I use a if/else loop for each button touched, it works great but I consider that to be inelegant and just plain cumbersome.

SO, I am using the button.tag to allow me to assign a number to each button then evaluate each button based on the .tag property.
As I mentioned before, works great with if/else if loops.

But I want to change the tag to a string and then nest it within a NSURL to play a sound. Each button will generate a new tag and consequently a new sound.

As reported by the debugger, I am getting Not a CFSTRING and 'out of bounds' messages and upon stepping through, crashes. Fun, but not blue screen.

Here is the code in question. I humbly request of the Objective C gurus to lend some insight!

int soundNumber = [sender tag];

//soundPick = [NSString stringWithFormat:@"%d", soundNumber]; //remmed out as an alternative try
NSString *soundPick = [[NSNumber numberWithInt:soundNumber] stringValue];

NSURL *aSoundURL = [NSURL fileURLWithPath:[mainBundle pathForResource:soundPick ofType:@"aif"]];   
playSound = [[AVAudioPlayer alloc] initWithContentsOfURL:aSoundURL error:&error];
if (!playSound) {
    NSLog(@"no Sound for that button: %@", [error localizedDescription]); 
} 
[playSound play];

*/

Thanks,

Neil

A: 

It almost sounds like you're programming behaviour in your buttons.

Why don't you just assign each button to a different IBOutlet/callback method? You won't need any if/else statements at all.

Shaggy Frog
You can even map them all to the *same* action method that builds the resource name dynamically based on properties of the sender, since you can get the sender object as an id in the method call.
Joe McMahon
You can, sure. Is that level of complexity required, all for the sake of using just a single action method?
Shaggy Frog
Well, if it's just "button A plays sound A", that seems pretty simple.
Joe McMahon
I guess we see simple in different ways. To me, using tags + a switch statement (which creates a long method body) is not as simple (readable) as having one action method per button.
Shaggy Frog
@yehnan - I didn't copy the entire code stream. However, it is declared;thanks though.@Shaggyfrog - If I understand you correctly, I could create a 1 buton to one sound relationship, but if I have 20 or more buttons, that is tedious to maintain.@Joe - that is exactly what I wish to do.. But I am using the button.tag property. However, I am having troubles evaluating the .tag datum. The sender info is type .id - how do I incorporate that information into the NSURL method I have illustrated?Thanks again! You guys rock
Neil
+1  A: 

I would create an array with all 20 file paths in it, then i would use the tag to index the array.

Aran Mulholland
A: 

On your IBActions, add something like this:

SystemSoundID theSound; 
NSString *pathToSound;

pathToSound = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"<filename>" ofType:@"<filetype>"] isDirectory:NO];

AudioServicesCreateSystemSoundID((CFURLRef)pathToSound, &theSound);
AudioServicesPlaySystemSound (theSound);

Then add this to your .h file and the the AudioToolbox.framework to your project:

#import <AudioToolbox/AudioToolbox.h>

Worked like a charm for me. Hope this helps.

Mike
Thanks! I got the code to work and I used the button.tag. Then, I named each sound file against the tag number..So, tag 1 = 1.aif, tag 2=2.aif etc... I used a paper chart to get started then off to the races.THe issue I had was how to get the button.tag as a string and I struggled until I figured it out:-(IBAction)buttonPressed:(id)sender;int soundTag = [sender tag];soundPick = [[NSString alloc] initWithFormat: @"%d", soundTag];Then took soundPick and stuck in the appropriate location in NSURL.. Thanks!
Neil
A: 

Thanks! I got the code to work and I used the button.tag. Then, I named each sound file against the tag number.. So, tag 1 = 1.aif, tag 2=2.aif etc... I used a paper chart to get started then off to the races. THe issue I had was how to get the button.tag as a string and I struggled until I figured it out: -(IBAction)buttonPressed:(id)sender; int soundTag = [sender tag]; soundPick = [[NSString alloc] initWithFormat: @"%d", soundTag]; Then took soundPick and stuck in the appropriate location in NSURL.. Thanks!

Neil
you should have put this in your post and marked this question as answered
Aran Mulholland