Objective-c - validate filled fields before publish item - ios

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

Related

iOS add pinpad to secure the app

I'm building an iPad app in where I need to implement a pin secure lock screen.
I have created a very simple hardcoded password 1234, see below:
- (IBAction)enterPassword
{
NSString *passwordString = [NSString stringWithFormat:#"1234"];
if ([passwordField.text isEqualToString:passwordString]) {
//hide password field
passwordField.hidden = YES;
//show main application view
[super viewDidLoad];
}
else {
// Password is incorrect
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Incorrect Password" message:#"This password is incorrect." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
}
}
Now, how can I implement an ability for user to change the password anytime they want to, so that they are not depended on the hardcoded password.
Also, when they are about to change the password they will be asked for the current old password in order to change it.
Please bare in mind that I'm a very new in iOS developing :(
Many thanks for your help.

Placeholders for variables in UIAlertView?

I want a UIAlertView to warn the user if there are no items matching his/her chosen search criteria. My initial idea was to use this code:
if (aOiCount == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No instances of %#",self.thisSpec.activityOfInterest message:#"Please select an activity or Cancel" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
The idea being to slip an actual activity name into the title, like in an NSLog string.
Unfortunately, this doesn't work. Compiler tells me Expected ":"
Is it possible to use a variable like this, and if so, how?
Thanks!
call this line
#"No instances of %#",self.thisSpec.activityOfInterest
in one NSString
NSString *alertstr=[NSString stringWithFormat:#"No instances of %#",self.thisSpec.activityOfInterest];
after that call your UIAlertView and then rearrange the word delegate:nil into delegate:self
if (aOiCount == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertStr message:#"Please select an activity or Cancel" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
I don't think this is valid Objective C syntax:
initWithTitle:#"No instances of %#",self.thisSpec.activityOfInterest
You need to wrap it in an NSString, or a self-contained form.
Consider one of the many NSString methods, such as stringWithFormat or construct one in a different way. Either way, you should pass a complete string here.

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]];

How To Save UILabel Information

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.

UITableView didSelectRowAtIndexPath error - when selecting item again

Loaded a list of items on to the UITableview and was able to click and show an alert for the row selected . But however after saying "ok" on the alert and i reclick on the already selected row my code breaks down saying "Thread 1:Program received signal:EXC_BAD_ACCESS".please take a look at the code below.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *playerselected = [exercises objectAtIndex:indexPath.row];
NSString *video = [playerselected valueForKey:#"video"];
NSString *msg = [[NSString alloc] initWithFormat:#"You have selected %#", video];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Player selected"
message:msg
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[video release];
[msg release];
}
PLease suggest me what could be the issue here.
Don't release video.
When you retrieve a value from an NSDictionary you don't own it unless you explicitly retain it.
To be more specific, when you retrieve your string it is still owned by the dictionary. When you release it, you are releasing an object that you do not own, resulting in it being over-released. As a result it is deallocated, and when you next try to access it the memory is no longer valid and your app crashes.

Resources