Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nickculbertson

macrumors regular
Original poster
Nov 19, 2010
226
0
Nashville, TN
Hello,
I am working on an app with two UIButtons and one UIImageView. When button 1 is pressed I would like touches on the UIImageView to play "sound1". When button 2 is pressed I want touches on the same UIImageView to play "sound2".

Code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location1 = [touch locationInView:touch.view];

if(CGRectContainsPoint(image1.frame, location1)) 
    {   [image1 setHighlighted:YES];
		NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"];
		myMusic=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
		[myMusic play];
		
	}else {
        [image1 setHighlighted:NO];
    }

Is there a way to change the pathForResouces when button 1 and button 2 are selected to "sound1" and "sound2"?

Thanks,
Nick
 
Is there a way to change the pathForResouces when button 1 and button 2 are selected to "sound1" and "sound2"?
Yes. pathForResource: is a parameter that takes an NSString as input. So, rather than passing in a hard-coded string, pass a reference to one whose value is determined by which button was pressed instead.
 
Yes. pathForResource: is a parameter that takes an NSString as input. So, rather than passing in a hard-coded string, pass a reference to one whose value is determined by which button was pressed instead.


So, is this getting close?

.h
Code:
NSString *myString;

.m
Code:
-(IBAction)button1press {
    NSString *myString=[[NSString alloc] initWithString:@"sound1"];
}
-(IBAction)button2press {
    NSString *myString=[[NSString alloc] initWithString:@"sound2"];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location1 = [touch locationInView:touch.view];

if(CGRectContainsPoint(image1.frame, location1)) 
    {   [image1 setHighlighted:YES];
		NSString *path = [[NSBundle mainBundle] pathForResource:(NSString *)myString ofType:@"wav"];
		myMusic=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
		[myMusic play];
		
	}else {
        [image1 setHighlighted:NO];
    }


Thanks,
Nick
 
Yes, but still some work to do:

1) Your button press methods are assigning to a local variable and not the ivar you've defined in your .h

1) I left this part out before. my bad

Code:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h> 
#import <AVFoundation/AVFoundation.h>


@interface FirstView : UIViewController <AVAudioPlayerDelegate> {

	AVAudioPlayer *myMusic;
	CGPoint *location1;
        UITouch *touch;
	IBOutlet UIImageView *image1;
	NSString *myString;
	IBOutlet UIButton *button1;
	IBOutlet UIButton *button2;
}
- (IBAction) button1press;
- (IBAction) button2press;


@property (nonatomic, retain) IBOutlet UIImageView *image1;
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;

@property(nonatomic, retain) AVAudioPlayer* myMusic;


@end

or did you mean that I should have put this...

Code:
 NSString *myString=[[NSString alloc] initWithString:@"sound1"];

in this method...

Code:
if(CGRectContainsPoint(image1.frame, location1)) 
    {   [image1 setHighlighted:YES];
		NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"];
		myMusic=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
		[myMusic play];
		
	}else {
        [image1 setHighlighted:NO];
    }

2) Why are you casting myString when you pass it to pathForResource:?

2) Monkey see, Monkey do. I found some code on the internet where someone did it like that. Clearly not the right approach. :)

Thanks,
Nick
 
1) I left this part out before. my bad

Code:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h> 
#import <AVFoundation/AVFoundation.h>


@interface FirstView : UIViewController <AVAudioPlayerDelegate> {

	AVAudioPlayer *myMusic;
	CGPoint *location1;
        UITouch *touch;
	IBOutlet UIImageView *image1;
	[COLOR="SeaGreen"][B]NSString *myString;[/B][/COLOR]
	IBOutlet UIButton *button1;
	IBOutlet UIButton *button2;
}
- (IBAction) button1press;
- (IBAction) button2press;


@property (nonatomic, retain) IBOutlet UIImageView *image1;
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;

@property(nonatomic, retain) AVAudioPlayer* myMusic;


@end

or did you mean that I should have put this...

Code:
 NSString *myString=[[NSString alloc] initWithString:@"sound1"];

in this method...

Code:
if(CGRectContainsPoint(image1.frame, location1)) 
    {   [image1 setHighlighted:YES];
		NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"];
		myMusic=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
		[myMusic play];
		
	}else {
        [image1 setHighlighted:NO];
    }
Neither, really. You've declared an ivar (instance variable) by putting that line (I've highlighted it in green) into the .h. But when you have a line like this in your method:
Code:
-(IBAction)button1press {
    [COLOR="SeaGreen"]NSString *myString=[[NSString alloc] initWithString:@"sound1"];[/COLOR]
}
you override the scope of the ivar with a variable of scope local only to that method. You should be getting warnings around this code. Don't ignore them. Plus, when you alloc something, it's your responsibility to release it as well. Make sure you think about when/where that should be done.

2) Monkey see, Monkey do. I found some code on the internet where someone did it like that. Clearly not the right approach. :)
Well, at least you're admitting where the fault is. :) Copy-and-paste is not programming. In general, if you don't understand why you're doing something, you probably shouldn't be doing it. Step back and make sure you're comfortable with the fundamentals.
 
Thanks Dejo!

I have 2 apps "in review" right now with several ivar warnings. It'll be interesting to see if they let them slide.


I got it figured out now. I added a UILabel like an on screen NSLog.

Code:
took out the .h string

in the .m

- (IBAction)button1press:(id)sender{
	label1.text = @"ONE";
}
- (IBAction)button2press:(id)sender{
	label1.text = @"TWO";
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location1 = [touch locationInView:touch.view];
	
    if(CGRectContainsPoint(image1.frame, location1)) 
    {	if (label1.text == @"ONE")
		{[image1 setHighlighted:YES];
		NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"];
		myMusic=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
			[myMusic play];}
	else if (label1.text == @"TWO"){[image1 setHighlighted:YES];
		NSString *path = [[NSBundle mainBundle] pathForResource:"sound2" ofType:@"wav"];
		myMusic=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
		[myMusic play];}
		
	}else {
        [image1 setHighlighted:NO];
    }

Thanks Again,
Nick
 
hello! I want to create a button whose name is stored in a NSString as:

NSString buttonname;

and to declared the button, I have no idea. I want to do:

UIButton buttonname *;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.