-5

Basically I want to create 48 button outlets. I have created the buttons in the interface builder and I want to be able to connect them to the outlets.

I need to be able play a sound when I press one of the buttons. But the sound file that is played should be customizable.

I want smth like:

//first create the outlets
for(int i=1;i<49;i++){
    IBOutlet UIButton [Nstring of type (@"but%d"), i];
    [add butt"i" to array];
}

//then connect the outlets to the buttons

//listen for buttons pressed
while(1){ //or the listener equivalent don't know exactly how i works
    for(int i=1;i<49;i++){
        if(array[i].pressed==TRUE){
              //if button is pressed play the according file
              playsound(sounds[i]);
        }
    }
}

I need to be able to easily change the file that is played Thank you

AWF4vk
  • 5,810
  • 3
  • 37
  • 70
Vlad Otrocol
  • 2,952
  • 7
  • 33
  • 55
  • This may help you - https://stackoverflow.com/questions/46731185/how-to-connect-3-uibutton-to-one-outlet – Krunal Oct 13 '17 at 14:06

2 Answers2

0

I want smth like:

Nope. Most likely you want something like that:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSInteger i = 0;
    for (NSInteger y = 0; y < 7; y++) {
        for (NSInteger x = 0; x < 7; x++) {
            i++;
            CGRect frame = CGRectMake(5 + x * 45, 5 + y * 65, 40, 60);
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = frame;
            button.tag = i;
            [button setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:button];
        }
    }
}

- (IBAction)buttonClicked:(UIButton *)sender {
    NSLog(@"Imagine the music button number %d will play soon", sender.tag);
}

I know, it's one extra button. Just let the 49th button play the stackoverflow jingle.

Seriously, you should read some basic documentation. Start with About Creating Your First iOS App. Apple has good docs, and there are hundreds of beginner tutorials out there.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
0

You probably need more practice with Xcode/Objective-C basics and idioms. Your approach is not valid (for instance, on iOS, we don't loop (while(1)) to listen for events).

Find some books, and you will be able to make your own soundboard soon.

If you want to persist, here is some hints : Assuming you place manually your buttons on the XIB view. Assign differents tag (for instance from 1000 to 1048) to every buttons and bind them with an action :

// In your .h file
- (IBAction)didTouchButton:(UIButton * )sender;

Then you need to implement the action :

// In you .m file
- (IBAction)didTouchButton:(UIButton * )sender {
NSString * fileName = [NSString stringWithFormat:@"%d", sender.tag];
NSURL *url = [[NSBundle mainBundle] URLForResource:fileName withExtension: @"mp3"];
if (!url){NSLog(@"file not found"); return;}
NSError *error;
self.audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error] ;
[audio play];
}

Create a property for self.audio (@property in your .h and @synthesize in your .m). Rename your 48 sounds with name from 1000.mp3 to 1048.mp3, and add them in your project. Add the framework AVFoundation.framework (target->build phase->Link binaries with librairies-> +).

When you click on button with tag N, it will play the sound with the name N.mp3.

Good luck

arnaud del.
  • 904
  • 12
  • 27
  • First of all thank you for being tolerant/understanding.I don't have access to my mac right now so I can't copy the code exactly that is why I used pseudoCode. I have googeled a lot and found lots of useful bits of information but I don't know how to put them together. From what I understand I should make an IBOutlet collection. I have an array of filenames, a function that plays a sound file (taking as input the name), and I want to assign that function to each button, but each button should play a different file. I need to assign the action in a loop as I have a lot of buttons. – Vlad Otrocol Feb 05 '12 at 21:07
  • From my view, you don't really need IBOutletCollection. If you work with an array of filename. I suppose that the first button will play the first item of your array. So you need an indirection. You can generalize : "The first button (with tag 1000) will play the first filename of the array (index 0)". You can deduce the index of the button (int index = sender.tag-1000) and get the filename (NSString * fileToPlay = [myArray objectAtIndex:(index)). Sorry, I can't put source code in comments. – arnaud del. Feb 05 '12 at 21:22