2011-12-10 16 views
0

私はMyBgMusic.hと呼ばれるシングルトンクラスを完成させました& MyBgMusic.m。そのシングルトンクラスをSecondViewControllerまたはXIBの残りの部分に参照する方法。ここでiOS:シングルトンクラスから音楽バックグラウンドを参照するには?

は私のシングルトンクラスである:

Hファイル:

#import <Foundation/Foundation.h> 
#import <AVFoundation/AVAudioPlayer.h> 

@interface MyBgMusic : UIViewController <AVAudioPlayerDelegate> { 


    AVAudioPlayer *player; 
    UIButton *playBgMusic; 


} 

@property (nonatomic, retain) IBOutlet AVAudioPlayer *player; 
@property (nonatomic, retain) IBOutlet UIButton *playBgMusic; 


+ (id)sharedManager; 

-(IBAction) toggleMusic; 


@end 

Mファイル:

#import "MyBgMusic.h" 

static MyBgMusic *sharedMyManager = nil; 

@implementation MyBgMusic 

@synthesize player,playBgMusic; 

#pragma mark - 
#pragma mark Singleton Methods 

+ (MyBgMusic*)sharedManager { 

    static MyBgMusic *sharedMyManager; 
    if(!sharedMyManager) { 
     static dispatch_once_t oncePredicate; 
     dispatch_once(&oncePredicate, ^{ 
      sharedMyManager = [[super allocWithZone:nil] init]; 
      }); 
     } 

     return sharedMyManager; 
} 

+ (id)allocWithZone:(NSZone *)zone {  

    return [self sharedManager]; 
} 


- (id)copyWithZone:(NSZone *)zone { 
    return self;  
} 

#if (!__has_feature(objc_arc)) 

- (id)retain { 

    return self;  
} 

- (unsigned)retainCount { 
    return UINT_MAX; //denotes an object that cannot be released 
} 


- (id)autorelease { 

    return self;  
} 

- (void)dealloc 
{ 
    [MyBgMusic release]; 
    [playBgMusic release]; 
    [player release]; 
    [super dealloc]; 
} 
#endif 

#pragma mark - 
#pragma mark Custom Methods 

- (IBAction)toggleMusic { 

    if ([self.player isPlaying] == YES) { 
     [self.player stop]; 
    } else { 
     [self.player play]; 
    } 
    self.playBgMusic.enabled = YES; 

} 

- (void)viewDidLoad 
{ 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; 
    self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    player.delegate = self; 
    [player play]; 
    player.numberOfLoops = -1; 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view from its nib. 
} 

SecondViewController.m(私は私ができるようにシングルトンクラスから参照したいです)オン/オフを押すときにバックグラウンドミュージックを台無しにすることなく、もう一度それを使用してください。)

- (IBAction)toggleMusic{ 

    if ([self.player isPlaying] == YES) { 
     [self.player stop]; 
    } else { 
     [self.player play]; 
    } 
    self.playBgMusic.enabled = YES; 

} 

は、私はこのように宣言しなければなりません:

-(IBAction) sharedMusic { 

    [[MyBgMusic sharedManager] toggleMusic]; // instance method shareManager not found. What does it mean? 

} 

答えて

0

それはあなたがIBActionで[[MyBgMusic sharedInstance] toggleMusic];を呼び出す必要がありSecondViewControllerで

[[MyBgMusic sharedInstance] toggleMusic]; 
+0

私はすでにIBActionでこのコードを持っている: - (IBAction)toggleMusic { IF([self.player isPlaying] == YES){ [self.playerストップ]。 } else { [self.player play]; } self.playBgMusic.enabled = YES; } – Amink

+0

このコードを配置する場所:[[MyBgMusic sharedInstance] toggleMusic]; – Amink

0

を呼び出しているコードで、その後XIBをフックアップし、あなたのSecondViewControllerにIBActionを作成します。あなたが音楽を切り替えるとき。ちょうどあなたがself.playerを使用したのと同じです。

+0

いいえ私はそれを実験しようとします – Amink

+0

あなたのコードをもう一度読んで、あなたが間違っていたことがあります。 1.MyBgMusicはUIViewControllerから継承してはいけませんが、NSObjectから継承するべきです。 2. UIButtonを削除します。3. AVPlayerをプログラムで初期化します。ViewDidLoadの内容をinitに移動します。 3. UIButtonをSecondViewController/FirstViewControllerに配置する必要があります。 4.シングルトンの使い方を少し読んでください。 –

0

あなたのクラスをインポートし、この行を使用してほしいところはどこでも、それを参照する必要があります。

[[MyBgMusic sharedManager] toggleMusic] 

必要であれば、あなたもプロパティを追加し、あなたのクラスのインスタンスを作成せずにそれを参照することができます。

+0

私はそれを実験しようとし、問題があれば元に戻します。 – Amink

関連する問題