How To Save UILabel Information - ios

I have a calculator that multiplies a value with a certain $ rate. The user can edit the $ rate, and the value is multiplied with the user-entered rate. Here is my code for this:
- (IBAction)edit
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Edit New Amount"
message:#"Enter new rate"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex) {
UITextField *field = [alertView textFieldAtIndex:0];
field.placeholder = #"Enter New Rate";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet];
if ([field.text rangeOfCharacterFromSet:set].location != NSNotFound)
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Only numbers are allowed in this field."delegate:self cancelButtonTitle:#"OK."otherButtonTitles:nil];
[errorAlert show];
rate.text=#"";
}
else
{
rate.text = field.text;
}
}
else
{
//Cancel
}
}
However, when the app closes and is restarted, the old rate is displayed, not the new user-entered rate. How can I save the new user-entered rate, so that it is retained within the app, even when it is restarted?

If you want the value to be retained after app is closed, then you got to store it somewhere in file system. Easiest and convenient way is to use NSUserDefaults:
//Storing in user defaults
[[NSUserDefaults standardUserDefaults] setObject:field.text forKey:RATE_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
//Retrieving (possibly in app start or viewdidload)
self.rate.text = [[NSUserDefaults standardUserDefaults] objectForKey:RATE_KEY];
One note on usage of NSUserDefaults is that it doesn't support storing every object type, it supports NSString,NSArray,NSNumber etc and primitive types. Since you used NSStrings in your code I assumed you can save it as string to defaults.

Have you tried creating a new variable to hold the new input as the user enters the new input, and then setting whatever the new input is to that new variable? It sounds like it is restarting from the last variable that was set in the input.

Related

How to check whether the UITextfield is empty string

I'm trying to build a login page for an app. Something went wrong when testing the login condition.
There are two textfields, username and password. When the Login button is clicked, alert shows when
1) either fields are empty
2) both filled.
The code reflects this below. But the result came out no matter that the textfield is empty or not, it only showed as textfields were filled ( only the else part run). So I assume there was something wrong with the if condition. But when I checked the if condition in other view, it worked.
Can anyone help?
if ([_pLoginIDField.text isEqualToString:#""] || [_pPasswordField.text isEqualToString:#""]) {
UIAlertView *error = [[UIAlertView alloc] initWithTitle:#"Yeah !" message:#"must complete all fields" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[error show];
}
else {
UIAlertView *error2 = [[UIAlertView alloc] initWithTitle:#"Oooops !" message:#"hhhhhhhhhh" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[error2 show];
}
Try this:
if (_pLoginIDField.text.length > 0 || _pPasswordField.text.length > 0)
NSString *text = [NSString stringWithString: nameUITextField.text];
if(text.length == 0){
}
You may try this,
you can count how many characters are in string, it is indirectly checking to null.
NSString *inputString = [NSString stringWithString: nameUITextField.text];
if(inputString.charachters.count == 0){
// inputString is Blank
// Do your stuff
}else{
//inputString is not blank, Do other Stuff.
}
the above condition satisfy if inputString is blank otherwise it will enter into else block.

Forgot Password code: Objective C

I'm building an iOS app and I need to provide a forgot password function. I have a db that stores registered users and their details(userArray). I have searched by email inputed so, how do I do a UIAlert that shows the found password??? below is code I tried but my if condition is wrong.
[self.theuser valueForKey:#"password"] is the retrieved password from the database and its a string.
- (IBAction)ForgotPassword:(id)sender {
if ([[self.theuser valueForKey:#"password"] > 0 ]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Success"
message: #"Your Password is:"
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
You need to check that the length of the string is higher than 0, because using this:
if ([self.theuser valueForKey:#"password"])
will always return true if the value is other than nil, meaning if you server code returns that the value of password is "" (two quote marks with no space in between) then the app will recognize that the value is not nil and display an alert with no text in it
So the final code should look something like this:
- (IBAction)ForgotPassword:(id)sender {
NSString *retrievedPassword = [self.theuser valueForKey:#"password"];
if ( retrievedPassword.length > 0) {
NSString *message = [NSString stringWithFormat:#"Your password is: %#", retrievedPassword];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Success"
message: message
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
}
Change:
#"Your Password is:"
to:
[NSString stringWithFormat:#"Your Password is: %#", [self.theuser valueForKey:#"password"]]
You can also change this:
if ([[self.theuser valueForKey:#"password"] > 0 ]) {
to this:
if ([self.theuser valueForKey:#"password"]) {
If it didn't exist, it would count as "false". Any other value is "true".

Objective-c - validate filled fields before publish item

In my application I send data to server and parse.
But I have to send image and you need to choose category.
When you don't choose category I show for user information.
But: how can I check if the user has filled in all the fields? In my app choose image and category?
This is my code with show error when you don't choose category and send action:
- (IBAction)submitItem:(UIButton *)sender {
if ([category isEqualToString:#""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Category 'All' is reserved" message:#"You need to choose different category" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
return;
}
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 1.0);
///// send image to server
NSString *urlString = urlSaveFullImageToServer;
Your change could be as simple as using an else statement...
- (IBAction)submitItem:(UIButton *)sender {
if ([category isEqualToString:#""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Category 'All' is reserved" message:#"You need to choose different category" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
return;
}
else {
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 1.0);
///// send image to server
NSString *urlString = urlSaveFullImageToServer;
...
Personally I think a better approach is not to enable the "send" button until all of the data is complete.
Set the submit button enabled property to NO and then when the various pieces of information are entered (the image, the category) check to see if everything is complete - once it is, set enabled to YES

How to set UITextField default value to user's previous input?

My iOS app starts off with a pop-up message (UIAlertView) where you have to enter text in a UITextField. I would like the default value of the UITextField to be equal to the user's previous input.
Example: at 1pm, the user types "Happy" and clicks OK. When he re-opens the app at 2pm, the Text Field already has "Happy" as default value, he just needs to click OK. At 3pm, he opens the app, the default value is "Happy" but he changes it to "Angry", and then clicks OK. At 4pm, "Angry" is the default value when he opens the app.
- (void)viewDidLoad
{
[super viewDidLoad];
//Pop-up TextField
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"How are you feeling ?" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * machineTextFieldInit = [alert textFieldAtIndex:0];
machineTextFieldInit.placeholder = #"Mood";
[alert show];
}
Many thanks in advance for your help and advice !
It appears what you're looking for is a method to persist a user's data across multiple app sessions. This can be done a number of different ways:
Core Data
User Defaults
Plist File
External Server
Since the code you've supplied looks rather simple, perhaps using the NSUserDefaults will be the way to go.
To store a value in NSUserDefaults use this bit of code when the user presses the button:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField * machineTextField = [alertView textFieldAtIndex:0];
NSString * input = machineTextField.text;
if (input != nil)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:input forKey:#"my.app.domain.machineTextField"];
[defaults synchronize];
}
}
And when you present the dialog, load the value from the defaults.
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString * previous = [defaults objectForKey:#"my.app.domain.machineTextField"];
//Pop-up TextField
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"How are you feeling ?" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * machineTextFieldInit = [alert textFieldAtIndex:0];
machineTextFieldInit.placeholder = previous ? previous : #"Mood";
[alert show];
}
The simplest answer I can think of is the store the previous value in a class property, then when the user clicks cancel, store the value into their property
#property NSString *previousValue;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == [alertView cancelButtonIndex]){
previousText = machineTextFieldInit.text;
}
}
Then on the init of your alertview test field set
machineTextFiledInit.text = previousValue;
Haven't tested and there may be an easier way :)
Also don't forget to set previousValue to a default value for when the user clicks the first time.
Edit: If you want it to store it passed the close of the app
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[coder encodeObject:previousValue forKey:#"previousVal"];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
previousValue = [coder decodeObjectForKey#"previousVal"];
}

How to save a video file with a specific name in Objective-C?

Firstly I have this code which saves my video file in the documents directory:
filePath = [NSString stringWithFormat:#"%#/Documents/%f.mp4", NSHomeDirectory(),[[NSDate date] timeIntervalSince1970]];
Secondly I have a UIAlertView that allows the user to enter the name they want for the video they are saving:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"GIVE YOURE VIDEO A NAME" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
And I have this to display what is entered:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
if (alertView.alertViewStyle == UIAlertViewStylePlainTextInput){
NSLog(#"Entered: %#",[[alertView textFieldAtIndex:0] text]);
}
}
else
{
NSLog(#"user pressed Button Indexed 1");
// Any action can be performed here
}
}
Know that I want to do is to save the video file with a name specified by text which was entered in to the UIAlertView. To do this I need to change this line: filePath = [NSString stringWithFormat:#"%#/Documents/%f.mp4", NSHomeDirectory(),[[NSDate date] timeIntervalSince1970]]; which saves the file as the current date and time to the entered text. However, I do not know how I would go about doing that.
Are you asking how to replace the date with the text entered ?
You only need to pass in the string, which you are already doing with the homeDirectory, like so
filePath = [NSString stringWithFormat:#"%#/Documents/%#.mp4", NSHomeDirectory(),[[alertView textFieldAtIndex:0] text]];

Resources