Ios 从每个视图控制器调用方法
我想从每个视图控制器调用此方法。但我不知道此方法将写入何处以及如何调用此方法Ios 从每个视图控制器调用方法,ios,iphone,Ios,Iphone,我想从每个视图控制器调用此方法。但我不知道此方法将写入何处以及如何调用此方法 -(void)playSound{ NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]]; NSData *data =[NSData dataWithContentsOfURL:url]; audioPlayer = [[AVAudioPlayer alloc] initW
-(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];
}
您可以创建一个
BaseViewController
并在BaseViewController.h
中声明此方法,并在BaseViewController.m
文件中实现,然后将所有ViewController
设置为BaseViewController
的子级
BaseViewController.h
@interface BaseViewController : UIViewController
-(void)playSound;
@end
@interface ViewController : BaseViewController
@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
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self playSound];
}
@end
现在在您的视图控制器.h
@interface BaseViewController : UIViewController
-(void)playSound;
@end
@interface ViewController : BaseViewController
@end
ViewController.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
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self playSound];
}
@end
您可以创建一个
BaseViewController
并在BaseViewController.h
中声明此方法,并在BaseViewController.m
文件中实现,然后将所有ViewController
设置为BaseViewController
的子级
BaseViewController.h
@interface BaseViewController : UIViewController
-(void)playSound;
@end
@interface ViewController : BaseViewController
@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
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self playSound];
}
@end
现在在您的视图控制器.h
@interface BaseViewController : UIViewController
-(void)playSound;
@end
@interface ViewController : BaseViewController
@end
ViewController.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
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self playSound];
}
@end
您可以创建一个类别:
@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];
您可以创建一个类别:
@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];
步骤1 创建一个BaseViewController
@interface BaseViewController : UIViewController
- (void) playSound;
@end
步骤2
在那个BaseViewController.m上
步骤3
#import "BaseViewController.h"
// Notice this class is a subclass of BaseViewController (parent)
@interface yourViewController : BaseViewController
@end
步骤-4
你可以直接打电话
- (void)viewDidLoad
{
[super viewDidLoad];
[self playSound];
}
步骤1 创建一个BaseViewController
@interface BaseViewController : UIViewController
- (void) playSound;
@end
步骤2
在那个BaseViewController.m上
步骤3
#import "BaseViewController.h"
// Notice this class is a subclass of BaseViewController (parent)
@interface yourViewController : BaseViewController
@end
步骤-4
你可以直接打电话
- (void)viewDidLoad
{
[super viewDidLoad];
[self playSound];
}
在appdelegate文件中声明可以提供示例创建单独的NSObject类并粘贴此方法。并在任何需要检查此链接的地方调用它-->您不能将此方法链接到任何地方,因为它具有依赖性-(audioPlayer在作用域之外声明,并且还分配了其委托属性)。一种方法是将其全部打包(包括audioPlayer)到一个singleton对象中,并在任何想要播放声音的类中导入singleton.h。在appdelegate文件中声明您能否提供示例创建一个单独的NSObject类并粘贴此方法。并在任何需要检查此链接的地方调用它-->您不能将此方法链接到任何地方,因为它具有依赖性-(audioPlayer在作用域之外声明,并且还分配了其委托属性)。一种方法是将它全部打包(包括audioPlayer)到一个singleton对象中,并在任何想要播放声音的类中导入singleton.h。