0

This might be a bit confusing so if it is just comment and I will try and rephrase it.

I have 2 ViewControllers (ViewController and Page2ViewController). I have AVFoundation framework working and for an example this is what I'm doing:

//In ViewController.m
[PlaySound1 play];
[PlaySound2 play];

//In Page2ViewController.m
[PlaySound3 play];
[PlaySound4 play];

Each ViewController have a stop function like this:

-(void)Stop{

[PlaySound1 stop];
[PlaySound2 stop];
[PlaySound3 stop];
[PlaySound4 stop];

PlaySound1.currentTime = 0.0;
PlaySound2.currentTime = 0.0;
PlaySound3.currentTime = 0.0;
PlaySound4.currentTime = 0.0;

When you press a button it uses the stop function and plays the sound. My problem is each ViewController will only stop the tracks they hold. I've tried importing the ViewControllers into each other, I've tried copy all the codes to link them to the files into each ViewController but that doesn't work either.

Any ideas?

Thanks.

Jack Moseley
  • 99
  • 1
  • 9
  • Where are your [PlaySound]s defined? How are you referring to them between your ViewControllers? Could you show some more code please? – Akaino Dec 23 '15 at 12:29
  • In each ViewController.h i have `AVAudioPlayer *PlaySound1;` and so on. In each .m I have `NSURL *Sound1URL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Sound1" ofType:@"mp3"]]; PlaySound1 = [[AVAudioPlayer alloc] initWithContentsOfURL:Sound1URL error:nil];`. Thats how I define each sound to it's file. – Jack Moseley Dec 23 '15 at 12:31
  • I've got that in every viewcontroller file to attempt to make it work – Jack Moseley Dec 23 '15 at 12:32
  • So you want to stop ViewController1.Playsound1 from ViewController2? How are your creating your second ViewController? You could pass a ViewController1 instance to your second ViewController within a segue for example. – Akaino Dec 23 '15 at 12:39
  • Yea. I would like to stop ViewController1 PlaySound1 from the second ViewController. I switch screens via swipe like so: – Jack Moseley Dec 23 '15 at 12:40
  • `UIStoryboard *iPhone6 = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *iPhone6Pg2 = [iPhone6 instantiateViewControllerWithIdentifier:@"iPhone6Pg1"]; [self presentViewController:iPhone6Pg2 animated:NO completion:nil];` – Jack Moseley Dec 23 '15 at 12:40

1 Answers1

1

You really want to check this example on a BOOL variable. This way you can send a ViewController instance also and call their methods:

Link: Passing data between ViewControllers

To pass a BOOL value from ViewControllerA to ViewControllerB we would do the following.

in ViewControllerB.h create a property for the BOOL

@property(nonatomic) BOOL *isSomethingEnabled; in ViewControllerA you need to tell it about ViewControllerB so use an

#import "ViewControllerB.h"

Then where you want to load the view eg. didSelectRowAtIndex or some IBAction you need to set the property in ViewControllerB before you push it onto nav stack.

ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.isSomethingEnabled = YES;
[self pushViewController:viewControllerB animated:YES];

This will set isSomethingEnabled in ViewControllerB to BOOL value YES.

Community
  • 1
  • 1
Akaino
  • 1,025
  • 6
  • 23
  • Thanks for the answer. How would I check if isSomethingEnabled is equal to YES in ViewControllerA? I've just tried an if statement but that doesn't work – Jack Moseley Dec 23 '15 at 12:51
  • I don't know if I understand correctly. You want to check for viewControllerA.isSomethingEnabled == YES in your viewControllerB? If so, do it. Should work. You need to pass a viewControllerA instance to your ViewControllerB though. Just like passing the BOOL in the example. – Akaino Dec 23 '15 at 12:55
  • When trying this code I got this in the first ViewController.m (I put this in a Action as it needs to be triggered when a button is pressed: `if (isSomethingEnabled == YES){}` but the error it gave me is: Use of undeclared identifier 'isSomethingEnabled' but I have imported the Page2ViewController.h. In my Page2ViewController.m I have: `Page2ViewController *Page2View = [[Page2ViewController alloc] initWithNib:@"Page2ViewController" bundle:nil]; Page2ViewController.isSomethingEnabled = YES; [self pushViewController:Page2View animated:YES];` but each line has an error. These are the – Jack Moseley Dec 23 '15 at 13:01
  • Do you have declared isSomethingEnabled in your viewController.h? – Akaino Dec 23 '15 at 13:03
  • errors: First line has an error of: `No visible @interface for 'Page2ViewController' declares the selector 'initWithNib:bundle:'`. Second line has an error of: `Property 'isSomethingEnabled' not found on object of type 'Page2ViewController'`. And the third line has an error of: `No visible @interface for 'Page2ViewController' declares the selector 'pushViewController:animated'` – Jack Moseley Dec 23 '15 at 13:04
  • These errors are somewhat self-explaining. Your Page2ViewController does not have the chosen selecter. Try initWithNibNAME. – Akaino Dec 23 '15 at 13:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98814/discussion-between-jack-moseley-and-akaino). – Jack Moseley Dec 23 '15 at 13:09