2016-08-17 4 views
1

Я хочу вызвать этот метод с каждого контроллера вида. Но я не знаю, где этот метод будет писать и как я могу назвать этот метод.Метод вызова от каждого контроллера представления

-(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]; 
} 
+0

объявить в AppDelegate файле – Birendra

+0

вы можете предоставить пример –

+0

Создать отдельный класс NSObject и вставить этот метод. И назовите его, где бы вы ни захотели – Blisskarthik

ответ

2

Вы можете создать BaseViewController и объявить этот метод внутри BaseViewController.h и реализовать внутри BaseViewController.m файла, чем установить все ваши ViewController как ребенок 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 

Теперь в вашем 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 
2

Вы можете создать категорию:

@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 

и вы можете позвонить в ваш контроллер представления:

[self playSound]; 
2

Step-1

создать один BaseViewController

@interface BaseViewController : UIViewController 

- (void) playSound; 

@end 

Шаг-2

на этом 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 

Шаг-3

#import "BaseViewController.h" 

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

шаг -4

вы можете позвонить непосредственно

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[self playSound]; 
} 
Смежные вопросы