无法从 Objective C、iPhone SDK 中的另一个视图控制器调用方法
在我的 StartWorkoutViewController.m 我触发:
#import "WorkoutViewController.h"
@implementation StartWorkoutViewController
- (IBAction) start
{
[firstViewController performSelector:@selector(callMyAction)];
WorkoutViewController *ViewCon = [[WorkoutViewController alloc]init];
[ViewCon callMyAction];
[ViewCon release];
}
然后在我的 WorkOutViewController.h 我添加了这个:
- (void)callMyAction;
在我的 WorkOutViewController.m 我添加了这个:
#import "StartWorkoutViewController.h"
-(void)viewWillAppear:(BOOL)animated{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/achtergrondgeluid.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
}
-(void)callMyAction{
NSLog(@"suckers");
[audioPlayer pause];
}
现在我的 NSLog (@"suckers") 从 StarWorkoutViewController 触发得很好,但由于某种原因,audioPlayer什么都不做,有什么想法吗?还可以通过此文件中的 callMyAction 在 Workout ViewController 中设置标签。别干活!有什么想法吗?谢谢!
At my StartWorkoutViewController.m I trigger:
#import "WorkoutViewController.h"
@implementation StartWorkoutViewController
- (IBAction) start
{
[firstViewController performSelector:@selector(callMyAction)];
WorkoutViewController *ViewCon = [[WorkoutViewController alloc]init];
[ViewCon callMyAction];
[ViewCon release];
}
then at my WorkOutViewController.h I added this:
- (void)callMyAction;
and at my WorkOutViewController.m I added this:
#import "StartWorkoutViewController.h"
-(void)viewWillAppear:(BOOL)animated{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/achtergrondgeluid.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
}
-(void)callMyAction{
NSLog(@"suckers");
[audioPlayer pause];
}
Now my NSLog (@"suckers") triggers very well from the StarWorkoutViewController, but for some reason the audioPlayer does nothing, any idea? Also setting labels in the Workout ViewController from the callMyAction in this file. Don't work! Any idea? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有发生任何事情的原因是因为您在播放器开始播放之前调用了暂停方法。 viewWillAppear 在显示在屏幕上之前不会被调用。实际上,即使那样,它也并不总是被调用,但这完全是另一个问题。因此,如果您想暂停播放器,它需要实际正在播放。如果您将该 viewWillAppear 代码放入 viewDidLoad 中,它可能会按预期工作。
目前您的代码正在执行以下操作:
The reason nothing is happening is because your are calling the pause method before the player even starts playing. viewWillAppear does not get called until it is visually on the screen. It actually doesn't always get called even then but that is another issue entirely. So if you want to pause the player it needs to actually be playing. If you put that viewWillAppear code in viewDidLoad it will probably work as expected.
Currently your code is doing this: