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
}
}
}
Related
This is my first iOS app and at this moment I'm struggling with Acccount / Login transitions on my storyboard.
What I'm trying to do is, If there is no logged user, display the login, otherwise display the account menu (Edit profile, My savings, and Logout).
Actually I'm achieving with this code on my AccountMenuViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
if (![self isLogged]) {
[self presentLoginView];
}
}
#pragma mark - Login methods
- (BOOL)isLogged
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [[defaults objectForKey:#"loggedIn"] boolValue];
}
- (void)presentLoginView
{
[self performSegueWithIdentifier:#"LoginSegueId" sender:self];
}
When the user touch "Cancel login" I'm trying to redirect to my MainView, I've make a segue but the Tab Bar is not displayed.
And when the login is ok, Close the login view.
I want to know your advices and tips, What are the best practices with this scenario?
This is my Storyboard.
First you should either make the login screen your rootViewController and just push to main view if
[self isLogged]==YES
or have the Login presented in a model fashion with the main view as a root, that way instead of a segue you can just dismiss it and the main view will be present underneath. you can also have the main view push the login and just hide the nav bar and use 'cancel login' as a sort of '< back' button that you would see on the nav bar by using the popViewController method..... also you can simplify the method:
- (BOOL)isLogged
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [[defaults objectForKey:#"loggedIn"] boolValue];
}
can be :
- (BOOL)isLogged
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [[NSUserDefaults standardUserDefaults] boolForKey:#"loggedIn"];
}
hope i helped!
I have a storyboard in my app with my UISwitch element (checklist) and I am attempting to store the switch states so that if the user checks a few items and then leaves the page (modally) or closes the app with the Home key, the state of the UISwitch elements is remembered and the user can continue along with the checklist.
If an item is read and you turn the UISwitch off, the font turns red else the UISwitch is on and the font is White.
Everything seems to work (still learning OOP and Xcode 5) except if I back out of the page (leave modally) and then return the UISwitch elements are all On again. If I stop the simulator (or IPAD) and restart the app when I get to the page all UISwitch are ON again instead of what was selected being OFF.
If I use the Home button and return to the app the UISwitch elements are still in the state they were left.
In the .m file here is the viewDidLoad method for just one of the switches:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
self.SwitchFlipChart.on = ([[standardDefaults stringForKey:#"Switch1"] isEqualToString:#"On"]) ? (YES) : (NO);
}
and below is the IBAction method for the same UISwitch element
- (IBAction)SwitchFlipchart:(UISwitch *)sender{
if (SwitchFlipchart.on) {
LabelFlipchart.textColor = [UIColor whiteColor];
} else {
LabelFlipchart.textColor = [UIColor redColor];
}
SwitchFlipchart.hidden = YES;
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
if (sender.tag == 0) {
if (sender.on == 0) {
[standardDefaults setObject:#"Off" forKey:#"Switch1"];
} else if (sender.on == 1) {
[standardDefaults setObject:#"On" forKey:#"Switch1"]; //removed f from fSwitch1
}
}
[standardDefaults synchronize];
}
Does it appear I am using the NSUserDefaults correctly or is it because I am reloading the page when stopping the Simulator or leaving the Page Modally?
Is there a better (sometimes easier is not better) way to work with NSUserDefaults and multiple UISwitch elements?
Is there a better way to retain UISwitch element state when loading a page after leaving it modally?
Is there a better way to store UISwitch states than the NSUserDefaults?
Replace your code with this
- (IBAction)SwitchFlipchart:(UISwitch *)sender{
if (SwitchFlipchart.on) {
LabelFlipchart.textColor = [UIColor whiteColor];
} else {
LabelFlipchart.textColor = [UIColor redColor];
}
SwitchFlipchart.hidden = YES;
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
if (sender.tag == 0) {
if (sender.on == 0) {
[standardDefaults setObject:#"Off" forKey:#"Switch1"];
} else if (sender.on == 1) {
[standardDefaults setObject:#"On" forKey:#"Switch1"];
}
}
[standardDefaults synchronize];
}
Explantion :-
You are setting on for wrong key. The key should be same Switch1 for both condition. But you are using fSwitch1 for On state. Please check your code.
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
}
}
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
I am making a utility application, and I have a label on my main view. Depending on the user's selection, that label should say one of two things. How can I use a segmented control to change the label text?
In your flipside, create an IBAction called segmentedControlChanged - and hook it up to the 'value changed' trigger on the segmented control.
At the top of your settings controller, declare this constant:
#define kSegmentKey #"SegmentSetting" // Call these whatever you want
In your segmentedControlChanged method, write to NSUserDefaults, like so:
- (IBAction)segmentedControlChanged:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:[sender selectedSegmentIndex] forKey:kSegmentKey];
}
In your main view controller's viewWillAppear, put the following code:
- (void)viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int setting = [defaults integerForKey:kSegmentKey];
if (setting == 0) {
myLabel.text = #"First Message";
}
else {
myLabel.text = #"Second Message";
}
}
You can:
(1) store the segmented control's value in a variable that's passed as a return value to the FlipsideViewControllerDidFinish method, or else
(2) store the value in some area accessible to both the Flipside view and the First view, such as [NSUserDefaults standardUserDefaults].