Reputation: 69
I have some trouble. So i have AvPlayer and UIButton with play/stop. Also: I have three UiViewControllers. I need that when I click on the first button on the first UIVIewController on the second controller, the third controller button is pressed also, respectively, on the contrary, too. How its make? Any proposition?
It's simple code - push on button - play URL Stream and also when push again stop music.
-(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];
}
}
Upvotes: 0
Views: 76
Reputation: 6844
First of all, are the 3 view controllers allocated and initialized at once? If not, I recommend you set a property on your AppDelegate
class, like this:
@interface AppDelegate
@property (nonatomic, assign) BOOL commonButtonPressed;
// All your code here
@end
And you can set this property like this:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.commonButtonPressed = YES; // or NO;
Then, from your UIViewController
classes:
- (void)viewWillAppear:(BOOL)animated {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.commonButtonPressed) {
// Logic of what happens to the button goes here.
}
}
Another way of doing this, without touching your AppDelegate
class is using NSUserDefaults
, like this:
[[NSUserDefaults standardDefaults] setBool:(<YES or NO>) forKey:@"commonButtonPressed"];
[[NSUserDefaults standardDefaults] synchronize];
You can read back the value like this:
BOOL buttonPressed = [[NSUserDefaults standardDefaults] boolForKey:@"commonButtonPressed"];
Upvotes: 0
Reputation: 1708
If you don't want to use nsnotifications then use protocol and notify other viewcontrollers by using delegates
Upvotes: 0
Reputation: 8423
If you have several controllers that you need to notify about an event on another controller you can use NSNotificationCenter
Eg. in one controller in ViewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playBtnClicked:)
name:@"BTN_CLICKED"
object:nil];
Also in the same controller define the selector e.g.
-(void)playBtnClicked:(NSNotification *)pNotification
{
// do something
}
In the other controller trigger it by using
[[NSNotificationCenter defaultCenter]
postNotificationName:@"BTN_CLICKED" object:nil];
Upvotes: 1