Reputation: 20736
I have a "mute sounds" button that I need to place in all view controllers and its state should be transferred between it. How can I achieve this behavior? Is there smth like template for all view contollers or should I place it and transfer its state manually?
Thanks in advance.
Upvotes: 0
Views: 56
Reputation: 12719
The best way is to create a Super Class
which will handle the mute button behavior all together. See the implementation.
Create a Super Class of type UIViewController
, say it BaseViewController
. So it should look like below
BaseViewController.h
#import <UIKit/UIKit.h>
@interface BaseViewController : UIViewController
@end
BaseViewController.m
#import "BaseViewController.h"
@interface BaseViewController ()
@end
@implementation BaseViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self prepareVolumeButton];
}
-(void)prepareVolumeButton{
//CREATE BUTTON
[self.view addSubView:self.muteButton];
[self.muteButton addTarget:self action:@selector(volumeAction:) forControlEvents:UIControlEventTouchUpInside];
self.muteButton.selected=[[[NSUserDefaults standardUserDefaults] objectForKey:@"volume"] boolValue];
}
-(IBAction)volumeAction:(id)sender{
UIButton *btn=(UIButton *)sender;
btn.selected=!btn.selected;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:btn.selected] forKey:@"volume"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
Now in the end, just change the Base Class(UIViewController).h
to BaseViewController
in all the UIViewControllers you want to add the volume button
.
Hope it helps. (Ask if any doubt)
Cheers.
Upvotes: 1
Reputation: 1021
I'd recommend using a Singleton (details here) to store the state of it (have a @property BOOL enabled
on it for example). But you have to place it manually on all screens and retrieve the state from the singleton on load.
One way to place it on all views is to create a UIViewController subclass (named MuteButtonViewController
for example), override viewDidLoad
and create the button in it. Then subclass MuteButtonViewController
for the UIViewControllers
where you need that mute button.
Upvotes: 1
Reputation: 25459
You can save it's state representation in NSUserDefaults when it changed and read it when you need.
The other solution can be a singleton class. The solution with singleton class is sometimes use in games. The class keep all settings. It's easy to change a setting and it's also easy to read it.
You can add notification or delegate to the singleton if the other classes needs to be notify when the button has changed.
Upvotes: 1