无法从 Objective C、iPhone SDK 中的另一个视图控制器调用方法

发布于 2024-10-17 16:55:23 字数 1143 浏览 3 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏雨凉 2024-10-24 16:55:23

没有发生任何事情的原因是因为您在播放器开始播放之前调用了暂停方法。 viewWillAppear 在显示在屏幕上之前不会被调用。实际上,即使那样,它也并不总是被调用,但这完全是另一个问题。因此,如果您想暂停播放器,它需要实际正在播放。如果您将该 viewWillAppear 代码放入 viewDidLoad 中,它可能会按预期工作。

目前您的代码正在执行以下操作:

  1. Load WorkoutViewController
  2. Pause Audio
  3. WorkourViewController 变得可见(实际上这可能永远不会发生,因为我没有看到它被添加到任何窗口或任何东西)。
  4. 播放音频。

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:

  1. Load WorkoutViewController
  2. Pause Audio
  3. WorkourViewController become visible (actually this may never happen as I am not seeing it being added to any windows or anything).
  4. Plays audio.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文