I have a problem for my xCode application.
My application is about record outcome. Thus the items values should be positive. I try to prevent record negative or zero value while choosing an item, but my code did not function.
This is my code to record an item
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Outcome Item Value" message:@"Please enter an outcome item value" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
}
else
{
OutcomeItem* newOutcomeItem = [NSEntityDescription insertNewObjectForEntityForName:@"OutcomeItem" inManagedObjectContext:self.managedObjectContext];
newOutcomeItem.outcomeName = self.selectedOutcomeItem.outcomeName;
newOutcomeItem.outcomeValue = [NSNumber numberWithDouble:[[alertView textFieldAtIndex:0].text doubleValue]];
if (newOutcomeItem.outcomeValue <= 0) {
NSLog(@"Invalid Value");
[self.navigationController popViewControllerAnimated:YES];
}
else
{
[self.delegate addOutcomeItem:newOutcomeItem];
[self.navigationController popViewControllerAnimated:YES];
}
}
}
my OutcomeItem.h is
#import<Foundation/Foundation.h>
#import<CoreData/CoreData.h>
@class OutcomeRecord;
@interface OutcomeItem : NSManagedObject
@property (nonatomic, retain) NSString *outcomeName;
@property (nonatomic, retain) NSNumber *outcomeValue;
@property (nonatomic, retain) OutcomeRecord *outcomeRecord;
@end;
OutcomeItem.m is
#import "OutcomeItem.h"
#import "OutcomeRecord.h"
@implementation OutcomeItem
@dynamic outcomeName;
@dynamic outcomeValue;
@dynamic outcomeRecord;
@end
Add outcome item method is
-(void)addOutcomeItem:(OutcomeItem *)outcomeItem
{ [self.currentOutcomeRecord addOutcomeItemsObject:outcomeItem]; self.outcomeRecordItemList = [self.currentOutcomeRecord.outcomeItems allObjects];
[self.tableView reloadData];
NSError* error;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Could not save outcome item insertion:\n%@", error.userInfo);
}
}