2012-11-27 120 views

回答

0

您应该使用的,而不是一个实例变量的属性,以便能够从其他类访问AVAudioPlayer

@interface MenuViewController : UIViewController { 
} 

@property (retain) AVAudioPlayer* audioPlayer; 

在其他视图控制器,你可以使用再拿到audioPlayer

[myMenuViewController audioPlayer] 
+0

我会在哪里放第二块代码 – user1612646

+0

我不知道如何在另一个视图中获得对'MenuViewController'的引用..您可以用变量名称'MenuViewController'来代替'myMenuViewController'被存储在 – s1m0n

0

这是OOP的一个功能。根据你想要做什么,你需要做两件事之一。要么在两个地方实例化对象,要么在一个地方实例化它并将其传递给另一个地方。如果你想通过它,你可以使用类似NSNotification。在将要使用共享audioPlayer你可以做类似的类:

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(incomingAudioPlayer:) 
               name:@"ShareAudioPlayer" 
               object:nil]; 
    } 
    return self; 
} 

- (void)incomingAudioPlayer:(NSNotification *)notification { 
    // do stuff here 
} 

而且在你实例audioPlayer(只是实例化后)类,你可以这样做:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ShareAudioPlayer" 
                  object:self 
                  userInfo:audioPlayer]; 

其实你可能需要使用userInfo并将audioPlayer粘贴到NSDictionary或其他东西中,但这会让你更接近。这是我通常使用的方法,可能还有其他方法。

相关问题