您可以创建一个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