Ios 如何在三个屏幕上使用相同的按钮?

Ios 如何在三个屏幕上使用相同的按钮?,ios,xcode,uiviewcontroller,uibutton,Ios,Xcode,Uiviewcontroller,Uibutton,我有点麻烦。所以我有AvPlayer和带播放/停止的UIButton。 另外:我有三个UIViewController。我需要的是,当我点击第二个控制器上的第一个UIVIewController上的第一个按钮时,第三个控制器按钮也会被分别按下,反之亦然。它是什么牌子的?有什么建议吗 这是一个简单的代码-按下按钮-播放URL流,并且当再次按下时停止音乐 -(IBAction)playRadioButton:(id)sender { if(clicked == 0) {

我有点麻烦。所以我有AvPlayer和带播放/停止的UIButton。 另外:我有三个UIViewController。我需要的是,当我点击第二个控制器上的第一个UIVIewController上的第一个按钮时,第三个控制器按钮也会被分别按下,反之亦然。它是什么牌子的?有什么建议吗

这是一个简单的代码-按下按钮-播放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];
    }
}

如果您有多个控制器需要通知另一个控制器上的事件,则可以使用
NSNotificationCenter

例如,在ViewDidLoad中的一个控制器中

[[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];

如果不想使用nsnotifications,请使用protocol并通过委托通知其他ViewController首先,是否立即分配并初始化了3个视图控制器?如果没有,我建议您在
AppDelegate
类上设置一个属性,如下所示:

@interface AppDelegate

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

@end
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.commonButtonPressed = YES; // or NO;
[[NSUserDefaults standardDefaults] setBool:(<YES or NO>) forKey:@"commonButtonPressed"];
[[NSUserDefaults standardDefaults] synchronize];
BOOL buttonPressed = [[NSUserDefaults standardDefaults] boolForKey:@"commonButtonPressed"];
您可以这样设置此属性:

@interface AppDelegate

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

@end
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.commonButtonPressed = YES; // or NO;
[[NSUserDefaults standardDefaults] setBool:(<YES or NO>) forKey:@"commonButtonPressed"];
[[NSUserDefaults standardDefaults] synchronize];
BOOL buttonPressed = [[NSUserDefaults standardDefaults] boolForKey:@"commonButtonPressed"];
然后,从
UIViewController
类:

- (void)viewWillAppear:(BOOL)animated {

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

        // Logic of what happens to the button goes here.
    }
}
另一种方法是使用
NSUserDefaults
,这样可以在不接触
AppDelegate
类的情况下执行此操作:

@interface AppDelegate

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

@end
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.commonButtonPressed = YES; // or NO;
[[NSUserDefaults standardDefaults] setBool:(<YES or NO>) forKey:@"commonButtonPressed"];
[[NSUserDefaults standardDefaults] synchronize];
BOOL buttonPressed = [[NSUserDefaults standardDefaults] boolForKey:@"commonButtonPressed"];

你的代码看起来不错。有什么问题吗?到目前为止,我只看到一个按钮,没有视图控制器。问题是,如果用户在一个视图控制器中点击“播放”,它会变为“停止”,然后移动到另一个视图控制器,按钮也会在那里显示“停止”?还有,你说的“我有三个视图控制器”是什么意思?它们是同时存在(UIDABBARCONTROLLER)还是在不同的时间存在(UINavigationController、显示视图控制器等)?这是一个很大的区别,因为您不能向还不存在的视图控制器发送消息。有什么需要理解的?我有三个屏幕。在第一个屏幕上,我放置按钮,在第二个和第三个屏幕上也放置按钮。当我按下第一个屏幕上的按钮时,第二个和第三个屏幕也应该按下。我没有输入控制器的代码,问题中根本不需要它。这是工作!当我按下按钮时,屏幕也发生了变化,播放了音乐,但我无法停止来自另一个控制器的播放按钮的音乐…我在VievDidLoad中的另一个ViewController中插入以下内容:[[NSNotificationCenter defaultCenter]postNotificationName:@“BTN_CLICKED”对象:nil];我想你必须发布另一个新问题,因为我失去了一点轨迹,而且我对
AVPLAYER
类也不是很熟悉。