Вы можете создать 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
объявить в AppDelegate файле – Birendra
вы можете предоставить пример –
Создать отдельный класс NSObject и вставить этот метод. И назовите его, где бы вы ни захотели – Blisskarthik