don't show again ViewController - ios

In my app, I created a first ViewController (in storyboard) called welcome, with all the instructions. At the end of the page, I wanted to insert a box that could be selected showing "don't show again". By clicking on this box, and going to the next page, the first viewController will vanish. When I reopen the app, the controller won't be shown again. The only way to show it is by going on settings and selecting the switch "repristinate original settings" or something like that. Please, can anyone help me? Thanks!
I found an example but is not what I want:
- (IBAction)leggi{
NSString *stringaTesto = campo.text;
testo.text = [[NSString alloc] initWithFormat:#"%#", stringaTesto];
NSString *testoInserito = testo.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:testoInserito forKey:#"ciao"];
[defaults synchronize];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *testoSalvato = [[NSUserDefaults standardUserDefaults] objectForKey:#"ciao"];
if (testoSalvato == nil) {
testo.text = #"Non hai ancora inserito il tuo nome";
} else {
testo.text = [[NSString alloc] initWithFormat:#"Ciao %#", testoSalvato];
}
}
The IB Outlet is linked to a button

store the don't show again as a BOOL in NSUserDefaults.. and check it before showing the view..
if it is TRUE..dont show.other wise show it.
edit
Lets say you have your app delegate and currently show your don't show view again (Lets say A) from it ..after which you show another view(Lets say B)
then in your app delegate you have to get a BOOL
like this
BOOL _Dont_Show_Again = [NSUSerDefaults standardDefaults] boolForkey : #"Don't Show"];
if(_Dont_Show_Again)
{
load B Code here...
}
else
{
load A Code here;
}
first time _Dont_Show_Again will be 0 since it does not exist in default..but if user select don't show you should save it in the default and this code will then work fine for you

Related

Saving segmented controller position

I am facing an issue that my segmented controller is not saving its position after closing the application and opening it again.
My code is as per below:
- (void)viewDidLoad {
[super viewDidLoad];
[self.segmentedControlButtonStyle addTarget:self action:#selector(changeButtonStyle:) forControlEvents:UIControlEventValueChanged];
}
- (IBAction)changeButtonStyle:(id)sender {
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:#"group.number.application"];
NSInteger selectedSegmentedControlerIndex = self.segmentedControlButtonStyle.selectedSegmentIndex;
if (sharedDefaults) {
[sharedDefaults setInteger: selectedSegmentedControlerIndex forKey:#"MySelectedButtonStyleKey"];
[sharedDefaults synchronize];
}
}
The funny thing is that NSUserDefaults actually is saving correct index because from method I provided above if I change button style it will keep changed after closing and opening application again because I can see it by fact but segmented controller itself is not showing correct segment.
I am not sure why this is happening because I am synchronizing after each segment change but still segmented controller keeps its default position.
in view did load you should add code to set your saved segment
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:#"group.number.application"];
int mySegment = [sharedDefaults integerForKey:#"MySelectedButtonStyleKey"];
if(mySegment) {
self.segmentedControlButtonStyle.selectedSegmentIndex = mySegment;
}

xcode - Label changes value when I change views

I have searched around and cannot find anything, help would be appreciated. I am very new to objective-C and Xcode.
In my app the player starts with 100 coins, this is represented in a label. When the user clicks a button to spend 10 coins, a popup box appears and asks 'are you sure', the user can click ok, or cancel.
If the user clicks 'ok' they spend 10 coins. At the moment, in the simulator when i'm in the same view everything is fine, the 100 goes down to 90 etc...
But when I go to another view and then back again, the coin amount goes back up to 100. This is the same when the user exits the app.
Here is my code:
.h file
//Coin
IBOutlet UILabel * coinCount;
.m file
int coinAmount = 100;
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"user pressed Cancel");
// Any action can be performed here
}
else
{
NSLog(#"user pressed OK");
coinAmount -= 10;
[coinCount setText:[NSString stringWithFormat:#"%d", coinAmount]];
NSString * string = [NSString stringWithFormat:#"%d", coinAmount];
//Save coin amount
NSString * saveCoinAmount = string;
NSUserDefaults * defaultsCoinAmount = [NSUserDefaults standardUserDefaults];
[defaultsCoinAmount setObject:saveCoinAmount forKey:#"saveCoinLabel"];
[defaultsCoinAmount synchronize];
}
}
That seems to save the new coin amount, so now when the user goes to another view and back i try and load the saved coin amount:
- (void)viewDidLoad
{
[super viewDidLoad];
//Coin Label
NSUserDefaults * defaultsLoadCoin = [NSUserDefaults standardUserDefaults];
NSString * loadCoinLabel = [defaultsLoadCoin objectForKey:#"saveCoinLabel"];
[coinCount setText:loadCoinLabel];
}
Any help would be greatly appreciated!
Your problem is that you are storing your coins in two places - an integer variable and the label. When you return to your view you restore the saved coin amount directly into the label, but when you perform the "purchase" you use the integer, which has been re-initialised to 100.
I also suggest you get out of the habit of using instance variables and use properties.
You should do something like this -
.m file
#interface MyClass () // Change this to suit your class name
#property NSInteger coinAmount;
#property (weak,nonatomic) IBOutlet UILabel *coinLabel;
#end
#implementation MyClass
- (void)viewDidLoad
{
[super viewDidLoad];
//Coin Label
NSUserDefaults * defaultsLoadCoin = [NSUserDefaults standardUserDefaults];
if ([defaultsLoadCoin objectForKey:#"coins] == nil) {
self.coinAmount=100;
[defaultsLoadCoin setInteger:self.coinAmount forKey:#"coins"];
}
else {
self.coinAmount = [defaultsLoadCoin integerForKey:#"coins"];
}
self.coinLabel.text=[NSString stringWithFormat:#"%ld",self.coinAmount];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"user pressed Cancel");
// Any action can be performed here
}
else
{
NSLog(#"user pressed OK");
self.coinAmount -= 10;
self.coinLabel.text=[NSString stringWithFormat:#"%ld",self.coinAmount];
//Save coin amount
NSString * saveCoinAmount = string;
NSUserDefaults * defaultsCoinAmount = [NSUserDefaults standardUserDefaults];
[defaultsCoinAmount setInteger:self.coinAmount forKey:#"coins"];;
[defaultsCoinAmount synchronize];
}
Your coinAmount property is not persistent through app launches or initialisations of the view controller in which it is created. You should consider persisting this value in database (like CoreData) or in NSUserDefaults.
My advice: start with basics (links to docs):
CoreData
NSUserDefaults

Saving users choices in NSUserDefaults

Hi im currently developing an app where the initial view controller is a screen with two buttons, one is for one thing and the other is for a nother thing. Each button leads to a new viewcontroller. What i want to do is for the app to recognize the users choice of button and the save that so when the user opens the app the next time it goes straight to the new viewcontroller.
I know i have to use NSUserDefaults but im pretty new to coding so if you guys could explain it it detail that would be perfect.
Thanks!
When clicks the button:
- (void) buttonPressed: (id) sender
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:#"Something" forKey:#"SOME_KEY"];
[userDefaults synchronize];
}
You can add this code to handle what you want:
- (void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([userDefaults objectForKey:#"SOME_KEY"] != nil)
{
if ([[userDefaults objectForKey:#"SOME_KEY"] isEqualToString:#"Something"])
{
// push one ViewController
}
else
{
// push the other one
}
}
}

how to access the value of a UISwitch

i have created a settingsView and also a view that gives you a choice to be notified if you want at a certain time its a yes or no but i do not know how to access the value of the uiswitch i know instead of trying to get the views object i should try the preference that the state has been saved to but i don't know how i just want to tap my UISwitch and then in the settingsView the UILabel gets filled but i don't know how to access the value on the UISwitch once tapped i have saved it in NSUserDefaults in the notifiedView but do not know how to access that value in the settingsView so i can put in the settingsView
if (UISwitch.on)
{
//notify me please
} else {
// leave as is
}
of course that's just a quick layout of how i want it to be though and also where do i put the it in the settingsView viewDidLoad, viewWillAppear or both please help im a new be as well so please be kind if i have said things that you would deem not the right syntax thanks
Try this code:
// add this code in your switch touch event
- (IBAction)YourSwitch:(UISwitch*)sender
{
if (UISwitch.on)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:true forKey:#"Sound"];
[defaults synchronize];
}
else
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:false forKey:#"Sound"];
[defaults synchronize];
}
}
Add this code in your viewdidload .
BOOL swithState = [[NSUserDefaults standardUserDefaults] boolForKey:#"Sound"];
if (swithState)
{
_ref_slider.on=true;
}
else
{
_ref_slider.on=false;
}
i hope this code is useful for you.
Just make a UISwitch property in your UIViewController and make sure you set it in the init/viewDidLoad of your class (from an IBOutlet or when you programmatically add it to your view). Then you can access the on-property of the UISwitch from anywhere within your class (and a load of extra features!) and use the if-statement as you want..
Apple Doc ref: https://developer.apple.com/library/ios/documentation/uikit/reference/UISwitch_Class/Reference/Reference.html
If your are using storyboard then hook up your UISwitch's action and write the below code in it.
- (IBAction)switchTapped:(id)sender
{
if ([sender isOn]) {
// your code
}
else
{
// your code
}
}

How to keep UITextField text when the view changes

I've got two UITextFields, the input of which I store into strings player1 and player2. These UITextFields are on a ViewController called by a popOver segue. How can I make the UITextFields keep displaying their text once the view has changed?
I tried textFieldOne.text = player1; in the viewDidLoad section of the ViewController to no avail. Any ideas?
If your loaded view's delegate isn't ViewController, your code wouldn't be executed. So be sure that your code is on the delegate of the loaded view. Use also [textFieldOne setText:player1]. It's always better to call the setter method instead of setting the ivar directly. Then be sure that your UITextField is not nil and correctly binded. Use textFieldOne = [[UITextField alloc] init] to initialise it. If your problem continues, try also [textFieldOne setText:self.player1]. Hope it helps..
EDIT :
Got the solution here. You should use NSUserDefaults so your player names are stored and can be used in each view and even after re-opening your app (if you don't want this you can erase the defaults at lunch. Here is your bunch of code you need to change :
hardOne.m :
- (void)viewDidLoad
{
[super viewDidLoad];
[hard1ON setOn:switchState animated:NO];
//read player names to user defaults
[textFieldOne setText:[[NSUserDefaults standardUserDefaults] stringForKey:#"player1"]];
[textFieldTwo setText:[[NSUserDefaults standardUserDefaults] stringForKey:#"player2"]];
}
- (IBAction) returnKey1
{
player1 = [textFieldOne text];
[players addObject:(player1)];
//set player1's name to user defaults
[[NSUserDefaults standardUserDefaults] setValue:[textFieldOne text] forKey:#"player1"];
}
- (IBAction) returnKey2
{
player2 = [textFieldTwo text];
[players addObject:(player2)];
NSLog(#"array: %#",players);
//set player2's name to user defaults
[[NSUserDefaults standardUserDefaults] setValue:[textFieldTwo text] forKey:#"player2"];
}

Resources