Ios 如何通过编程将噪波从另一个ViewController中的UITableView分配给按钮?
我有一组声音填充了一个TableView。当用户选择其中一个时,我希望将该声音分配给另一个视图控制器中的按钮。该视图控制器当前正在使用分配给按钮的“默认”声音。最好的方法是什么 编辑我已编辑以显示所做的更改,但似乎仍然不起作用。有什么想法吗 pickSound.mIos 如何通过编程将噪波从另一个ViewController中的UITableView分配给按钮?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一组声音填充了一个TableView。当用户选择其中一个时,我希望将该声音分配给另一个视图控制器中的按钮。该视图控制器当前正在使用分配给按钮的“默认”声音。最好的方法是什么 编辑我已编辑以显示所做的更改,但似乎仍然不起作用。有什么想法吗 pickSound.m @interface pickSound () @end @implementation pickSound - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBu
@interface pickSound ()
@end
@implementation pickSound
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.titleObjectsArray = @[@"Horn", @"Dog Barking", @"Whistle", @"Funny Noises",];
self.soundObjectsArray = @[@"horn.wav", @"bark1.wav", @"whistle.wav", @"funny.wav",];
// Do any additional setup after loading the view.
}
-(void)viewDidAppear: (BOOL)animated{
[self.navigationController setNavigationBarHidden:NO animated:YES];
[super viewWillAppear:YES];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.titleObjectsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIButton *previewSound = [[UIButton alloc]init];
UIImage *btnImage = [UIImage imageNamed:@"PlayButtonPNG.png"];
[previewSound setImage:btnImage forState:UIControlStateNormal];
cell.textLabel.text = [self.titleObjectsArray objectAtIndex:indexPath.row];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row ==0)
{
sound = @"Horn";
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqual:@"pickedSound"]) {
ViewController *cameraVC = (ViewController *)segue.destinationViewController;
cameraVC.soundId = sound;
}
}
- (IBAction)playSound:(id)sender {
if ([selectedSound isEqualToString:@"Horn"]) {
NSURL *makeNoiseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"horn" ofType:@".wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)makeNoiseURL, &SoudId);
AudioServicesPlaySystemSound(SoudId);
} else if ([selectedSound isEqualToString:@"Dog Barking"]) {
NSURL *makeNoiseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"bark1" ofType:@".wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)makeNoiseURL, &SoudId);
AudioServicesPlaySystemSound(SoudId);
}
}
ViewController.m(播放声音的按钮所在位置)
#导入“ViewController.h”
#导入“pictureViewController.h”
#进口
@界面视图控制器()
@结束
@合成声音;
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
self.soundId=soundId;
if([soundId isEqualToString:@“horn”]){
NSURL*makeNoiseURL=[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@.wav“]类型的“horn”;
AudioServicesCreateSystemSoundID((uu桥CFURLRef)makeNoiseURL和SoudID);
}否则{
NSURL*makeNoiseURL=[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@.wav“]类型的“horn”;
AudioServicesCreateSystemSoundID((uu桥CFURLRef)makeNoiseURL和SoudID);
}
}
-(iAction)makeNoise:(id)发送方{
AudioServicesPlaySystemSound(SoudID);
}
在ViewController类中创建一个NSString属性,并在TableView中进行选择时进行设置。从viewDidLoad中删除硬编码的资源加载,并将此属性用作资源名称。我找到了它。我创建了一个NSString,它被指定了所选行的值。使用prepareforsegue将该字符串传递给第二个VC。使用if语句根据NSString sound为按钮指定要播放的声音
**皮克桑德**
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIndexPath = [self.titleObjectsArray objectAtIndex:indexPath.row];
if ([self.selectedIndexPath isEqual:@"Horn"]) {
sound = @"Horn";
} else if ([self.selectedIndexPath isEqual: @"Dog Barking"]) {
sound = @"Dog Barking";
}
[self performSegueWithIdentifier:@"newVC" sender:self];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DestinationVC *controller = segue.destinationViewController;
controller.selectedSound = sound;
}
viewcontroller.m
@interface pickSound ()
@end
@implementation pickSound
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.titleObjectsArray = @[@"Horn", @"Dog Barking", @"Whistle", @"Funny Noises",];
self.soundObjectsArray = @[@"horn.wav", @"bark1.wav", @"whistle.wav", @"funny.wav",];
// Do any additional setup after loading the view.
}
-(void)viewDidAppear: (BOOL)animated{
[self.navigationController setNavigationBarHidden:NO animated:YES];
[super viewWillAppear:YES];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.titleObjectsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIButton *previewSound = [[UIButton alloc]init];
UIImage *btnImage = [UIImage imageNamed:@"PlayButtonPNG.png"];
[previewSound setImage:btnImage forState:UIControlStateNormal];
cell.textLabel.text = [self.titleObjectsArray objectAtIndex:indexPath.row];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row ==0)
{
sound = @"Horn";
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqual:@"pickedSound"]) {
ViewController *cameraVC = (ViewController *)segue.destinationViewController;
cameraVC.soundId = sound;
}
}
- (IBAction)playSound:(id)sender {
if ([selectedSound isEqualToString:@"Horn"]) {
NSURL *makeNoiseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"horn" ofType:@".wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)makeNoiseURL, &SoudId);
AudioServicesPlaySystemSound(SoudId);
} else if ([selectedSound isEqualToString:@"Dog Barking"]) {
NSURL *makeNoiseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"bark1" ofType:@".wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)makeNoiseURL, &SoudId);
AudioServicesPlaySystemSound(SoudId);
}
}
我想我明白你在说什么,但由于某种原因,我做这件事时无法播放声音。见上面编辑的文章。有什么想法吗?在你的prepareForSegue方法中,设置一个断点,看看你是否在声音变量中得到了正确的值。在ViewController类中,不需要这行代码“self.soundID=soundID”,因为您已经从源类中设置了此属性。放置一个断点并检查数据是否正确。我看到您只为第一行硬编码了它(索引0)。确保你在正确的一排。