0

私はいくつかの問題を抱えています。だから私は再生/停止を伴うAvPlayerとUIButtonを持っています。また: 私は 3 つの UiViewControllers を持っています。2番目のコントローラーの最初のUIVIewControllerの最初のボタンをクリックすると、3番目のコントローラーボタンもそれぞれ逆に押される必要があります。その作り方は?何か案は?

それは単純なコードです - ボタンを押す - URL ストリームを再生し、もう一度押すと音楽を停止します。

-(IBAction)playRadioButton:(id)sender
{
    if(clicked == 0) {    
        clicked = 1;
        NSLog(@"Play");
        NSString *urlAddress = @"http://URLRADIOSTREAM";
        NSURL *urlStream = [NSURL URLWithString:urlAddress];
        myplayer = [[AVPlayer alloc] initWithURL:urlStream];
        [myplayer play];
        [playRadioButton setTitle:@"Pause" forState:UIControlStateNormal];
    }
    else
    {
        NSLog(@"Stop");
        [myplayer release];
        clicked = 0;
        [playRadioButton setTitle:@"Play" forState:UIControlStateNormal];
    }
}
4

3 に答える 3

1

別のコントローラーのイベントについて通知する必要がある複数のコントローラーがある場合は、使用できますNSNotificationCenter

例えば。ViewDidLoad の 1 つのコントローラーで

[[NSNotificationCenter defaultCenter]    addObserver:self 
                                            selector:@selector(playBtnClicked:)
                                                name:@"BTN_CLICKED"
                                              object:nil]; 

また、同じコントローラーでセレクターを定義します。

-(void)playBtnClicked:(NSNotification *)pNotification
{
// do something
}

他のコントローラーでは、使用してトリガーします

    [[NSNotificationCenter defaultCenter] 
                    postNotificationName:@"BTN_CLICKED" object:nil];
于 2013-05-18T23:11:49.603 に答える
0

nsnotifications を使用したくない場合は、プロトコルを使用し、デリゲートを使用して他のビューコントローラーに通知します

于 2013-05-19T00:06:15.707 に答える
0

まず、3つのView Controllerが一度に割り当てられて初期化されていますか?AppDelegateそうでない場合は、次のようにクラスにプロパティを設定することをお勧めします。

@interface AppDelegate

@property (nonatomic, assign) BOOL commonButtonPressed;
// All your code here

@end

そして、このプロパティを次のように設定できます。

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.commonButtonPressed = YES; // or NO;

次に、UIViewControllerクラスから:

- (void)viewWillAppear:(BOOL)animated {

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (appDelegate.commonButtonPressed) {

        // Logic of what happens to the button goes here.
    }
}

AppDelegateクラスに触れずにこれを行う別の方法はNSUserDefaults、次のように を使用することです。

[[NSUserDefaults standardDefaults] setBool:(<YES or NO>) forKey:@"commonButtonPressed"];
[[NSUserDefaults standardDefaults] synchronize];

次のように値を読み取ることができます。

BOOL buttonPressed = [[NSUserDefaults standardDefaults] boolForKey:@"commonButtonPressed"];
于 2013-05-19T00:51:39.513 に答える