从TableViewController更新内容

时间:2014-07-26 04:35:50

标签: ios objective-c uitableview

我有一个表视图,其中包含用户可以更新的不同信息。其中之一是“试点信息”。当用户点击此行时,他们将转到另一个视图控制器,在那里他们填写有关飞行员的信息。 (姓名电话号码等)然后他们可以点击保存或取消两者将它们返回到上一个表视图控制器。我想要做的是让用户能够再次点击导频信息,并查看他们刚输入的数据并在他们选择时进行编辑。

我的解决方案是添加另一个segue并使用以下代码为其提供唯一标识符。 (pilot是我的核心数据模型中实体的名称,updatepilot是我给segue的名称。)并更新实现文件中的ViewDidLoad。但是,当我在没有错误的情况下运行此操作时,我看不到保存的信息。任何帮助将不胜感激。

http://tinypic.com/r/2ihbera/8

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"UpdatePilot"]) {
        NSManagedObject *selectedPilot = [self.pilots objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
        PilotViewController *destViewController = segue.destinationViewController;
        destViewController.pilot = selectedPilot;
    }
}

#import "PilotViewController.h"

@interface PilotViewController ()

@end

@implementation PilotViewController
- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

@synthesize pilot;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (self.pilot) {
        [self.nameField setText:[self.pilot valueForKey:@"pilotName"]];
        [self.emailField setText:[self.pilot valueForKey:@"pilotEmail"]];
        [self.phoneField setText:[self.pilot valueForKey:@"pilotPhone"]];
        [self.insuranceField setText:[self.pilot valueForKey:@"pilotInsurance"]];



    }
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//Code created for cancel and save buttons
- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)save:(id)sender {

    NSManagedObjectContext *context = [self managedObjectContext];

    if (self.pilot) {
        // Update existing Pilot
        [self.pilot setValue:self.nameField.text forKey:@"pilotName"];
        [self.pilot setValue:self.phoneField.text forKey:@"pilotPhone"];
        [self.pilot setValue:self.insuranceField.text forKey:@"pilotInsurance"];
        [self.pilot setValue:self.emailField.text forKey:@"pilotEmail"];

    } else {
        // Create a new pilot
        NSManagedObject *newPilot = [NSEntityDescription insertNewObjectForEntityForName:@"Pilot" inManagedObjectContext:context];
        [newPilot setValue:self.nameField.text forKey:@"pilotName"];
        [newPilot setValue:self.phoneField.text forKey:@"pilotPhone"];
        [newPilot setValue:self.insuranceField.text forKey:@"pilotInsurance"];
         [newPilot setValue:self.emailField.text forKey:@"pilotEmail"];
    }

    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

0 个答案:

没有答案