我目前正在开发一个需要具有初始设置屏幕(名称、日期、存款金额和百分比)的应用程序。我创建了一个视图控制器,它将添加然后保存到核心数据。我也可以检索信息。
我接下来需要的是能够弹出另一个视图控制器并为同一个人输入连续号码。每当我将数据输入到第二个视图控制器中时,它就会作为添加返回,就好像它是一个新人一样。我需要一些帮助来解决这个问题,因为我还是新手,还在学习。
下面是两个视图控制器的 .h 和 .m 文件。
ForexDetailViewController.h
#import <UIKit/UIKit.h>
@interface ForexDetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *startamountTextField;
@property (weak, nonatomic) IBOutlet UITextField *startmonthTextField;
@property (strong, nonatomic) IBOutlet UITextField *startpercentTextField;
@property (weak, nonatomic) IBOutlet UITextField *endmonth1year1TextField;
@property (weak, nonatomic) IBOutlet UITextField *endpercentmonth1year1TextField;
@property (strong) NSManagedObject *device;
- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;
- (IBAction)textFieldReturn:(id)sender;
- (IBAction)calcultae;
@end
ForexDetailViewController.m
#import "ForexDetailViewController.h"
@interface ForexDetailViewController ()
@end
@implementation ForexDetailViewController
- (IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
@synthesize device;
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if (self.device) {
[self.nameTextField setText:[self.device valueForKey:@"name"]];
[self.startamountTextField setText:[self.device valueForKey:@"startamount"]];
[self.startmonthTextField setText:[self.device valueForKey:@"startmonth"]];
[self.startpercentTextField setText:[self.device valueForKey:@"startpercent"]];
[self.endmonth1year1TextField setText:[self.device valueForKey:@"endmonth1year1"]];
[self.endpercentmonth1year1TextField setText:[self.device valueForKey:@"endpercentmonth1year1"]];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
if (self.device) {
// Update existing device
[self.device setValue:self.nameTextField.text forKey:@"name"];
[self.device setValue:self.startamountTextField.text forKey:@"startamount"];
[self.device setValue:self.startmonthTextField.text forKey:@"startmonth"];
[self.device setValue:self.startpercentTextField.text forKey:@"startpercent"];
[self.device setValue:self.endmonth1year1TextField.text forKey:@"startpercent"];
[self.device setValue:self.endpercentmonth1year1TextField.text forKey:@"endpercentmonth1year1"];
} else {
// Create a new device
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Calculations" inManagedObjectContext:context];
[newDevice setValue:self.nameTextField.text forKey:@"name"];
[newDevice setValue:self.startamountTextField.text forKey:@"startamount"];
[newDevice setValue:self.startmonthTextField.text forKey:@"startmonth"];
[newDevice setValue:self.startpercentTextField.text forKey:@"startpercent"];
[newDevice setValue:self.endmonth1year1TextField.text forKey:@"endmonth1year1"];
[newDevice setValue:self.endpercentmonth1year1TextField.text forKey:@"endpercentmonth1year1"];
}
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
时间线视图控制器.h
#import <UIKit/UIKit.h>
@interface TimeLineViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *startamountTextField;
@property (weak, nonatomic) IBOutlet UITextField *endmonth1year1TextField;
@property (weak, nonatomic) IBOutlet UITextField *endpercentmonth1year1TextField;
@property (strong) NSManagedObject *device;
- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;
- (IBAction)textFieldReturn:(id)sender;
@end
时间线视图控制器.m
#import "TimeLineViewController.h"
@interface TimeLineViewController ()
@end
@implementation TimeLineViewController
- (IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
@synthesize device;
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if (self.device) {
[self.startamountTextField setText:[self.device valueForKey:@"startamount"]];
[self.endmonth1year1TextField setText:[self.device valueForKey:@"endmonthe1year1"]];
[self.endpercentmonth1year1TextField setText:[self.device valueForKey:@"endpercentmonth1year1"]];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
if (self.device) {
// Update existing device
[self.device setValue:self.startamountTextField.text forKey:@"startamount"];
[self.device setValue:self.endmonth1year1TextField.text forKey:@"endmonth1year1"];
[self.device setValue:self.endpercentmonth1year1TextField.text forKey:@"endpercentmonth1year1"];
} else {
// Create a new device
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Calculations" inManagedObjectContext:context];
[newDevice setValue:self.startamountTextField.text forKey:@"startamount"];
[newDevice setValue:self.endmonth1year1TextField.text forKey:@"endmonth1year1"];
[newDevice setValue:self.endpercentmonth1year1TextField.text forKey:@"endpercentmonth1year1"];
}
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
ForexViewController.m
#import "ForexViewController.h"
#import "ForexDetailViewController.h"
@interface ForexViewController ()
@property (strong) NSMutableArray *devices;
@end
@implementation ForexViewController
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Calculations"];
self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.devices.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"name"]]];
[cell.detailTextLabel setText:[device valueForKey:@"startamount"]];
[cell.detailTextLabel setText:[device valueForKey:@"startmonth"]];
[cell.detailTextLabel setText:[device valueForKey:@"startpercent"]];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.devices objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
// Remove device from table view
[self.devices removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
}
#pragma mark - Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"UpdateDevice"]) {
NSManagedObject *selectedDevice = [self.devices objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
ForexDetailViewController *destViewController = segue.destinationViewController;
destViewController.device = selectedDevice;
}
}
@end