IBOutlet 数组声明

发布于 2025-01-02 21:30:07 字数 636 浏览 0 评论 0原文

基本上我想创建 48 个按钮插座。我已经在界面生成器中创建了按钮,并且希望能够将它们连接到插座。

我需要在按下其中一个按钮时能够播放声音。但播放的声音文件应该是可定制的。

我想要这样的东西:

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

//然后将插座连接到按钮

//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]);
        }
    }
}

我需要能够轻松更改播放的文件 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的那抹悲凉 2025-01-09 21:30:07

您可能需要更多地练习 Xcode/Objective-C 基础知识和习惯用法。
您的方法无效(例如,在 iOS 上,我们不循环 (while(1)) 来侦听事件)。

找一些书,很快你就能制作自己的音板了。

如果你想坚持,这里有一些提示:
假设您手动将按钮放置在 XIB 视图上。
为每个按钮分配不同的标签(例如从 1000 到 1048),并将它们与一个操作绑定:

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

然后您需要实现该操作:

// 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];
}

为 self.audio 创建一个属性(.h 中的 @property 和 .m 中的 @synthesize) )。
将 48 个声音的名称从 1000.mp3 重命名为 1048.mp3,并将它们添加到您的项目中。
添加框架 AVFoundation.framework(目标 -> 构建阶段 -> 将二进制文件与库链接 -> +)。

当您单击带有标签 N 的按钮时,它将播放名称为 N.mp3 的声音。

祝你好运

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

撩心不撩汉 2025-01-09 21:30:07

我想要这样的东西:

不。您很可能想要这样的东西:

- (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);
}

我知道,这是一个额外的按钮。只需让第 49 个按钮播放 stackoverflow 歌曲即可。

说真的,您应该阅读一些基本文档。从关于创建您的第一个 iOS 应用程序。苹果有很好的文档,并且有数百个初学者教程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文