简体   繁体   中英

Call method from each view controller

I want to call this method from each view controller.But I do not know where this method will write and how i can call this method.

-(void)playSound{

NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]];
NSData *data =[NSData dataWithContentsOfURL:url];
audioPlayer  = [[AVAudioPlayer alloc] initWithData:data error:nil];
audioPlayer.delegate = self;
[audioPlayer setNumberOfLoops:0];
[audioPlayer play];
}

You can create one BaseViewController and declare this method inside BaseViewController.h and implement inside BaseViewController.m file, than set all your ViewController as a child of BaseViewController .

BaseViewController.h

@interface BaseViewController : UIViewController

-(void)playSound;

@end

BaseViewController.m

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

-(void)playSound {
     NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]];
     NSData *data =[NSData dataWithContentsOfURL:url];
     audioPlayer  = [[AVAudioPlayer alloc] initWithData:data error:nil];
     audioPlayer.delegate = self;
     [audioPlayer setNumberOfLoops:0];
     [audioPlayer play];
}
@end

Now in your viewController.h

@interface ViewController : BaseViewController

@end

ViewController.m

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self playSound];
}
@end

You can create a category:

@interface UIViewController (UIViewControllerAudio)

-(void)playSound;

@end


@implementation UIViewController (UIViewControllerAudio)

- (void)playSound{
    NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]];
    NSData *data =[NSData dataWithContentsOfURL:url];
    audioPlayer  = [[AVAudioPlayer alloc] initWithData:data error:nil];
    audioPlayer.delegate = self;
    [audioPlayer setNumberOfLoops:0];
    [audioPlayer play];
}

@end

and the you can call in your view controller:

[self playSound];

Step-1

create the one BaseViewController

@interface BaseViewController : UIViewController

 - (void) playSound;

 @end

Step-2

on that BaseViewController.m

@implementation BaseViewController

-(void)playSound{

NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]];
NSData *data =[NSData dataWithContentsOfURL:url];
audioPlayer  = [[AVAudioPlayer alloc] initWithData:data error:nil];
audioPlayer.delegate = self;
[audioPlayer setNumberOfLoops:0];
[audioPlayer play];
}

@end

Step-3

  #import "BaseViewController.h"

// Notice this class is a subclass of BaseViewController (parent)
@interface yourViewController : BaseViewController
@end

step -4

you can call directly

- (void)viewDidLoad
{
[super viewDidLoad];
 [self playSound];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM