Core Data : update record

TableViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    
    EditViewController *editViewController = [[EditViewController alloc] initWithNibName:@"EditViewController" bundle:nil];
    editViewController.people=[self.peopleArray objectAtIndex:indexPath.row];
    editViewController.context=self.context;
    [self.navigationController pushViewController:editViewController animated:YES];
     
}

EditViewController interface


EditViewController.h

//
//  EditViewController.h
//  CoreDataTutorial
//
//  Created by Jason on 29/05/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "People.h"

@interface EditViewController : UIViewController
@property (nonatomic,strong) IBOutlet UITextField *nameTextField;
@property (nonatomic,strong) IBOutlet UITextField *numberTextField;
@property (nonatomic,strong) People *people;
@property (nonatomic,strong) NSManagedObjectContext *context;

- (IBAction)save:(id)sender;

@end

EditViewController.m

//
//  EditViewController.m
//  CoreDataTutorial
//
//  Created by Jason on 29/05/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "EditViewController.h"

@interface EditViewController ()<UITextFieldDelegate,UIAlertViewDelegate>

@end

@implementation EditViewController
@synthesize nameTextField=_nameTextField;
@synthesize numberTextField=_numberTextField;
@synthesize context=_context;
@synthesize people=_people;

-(IBAction)save:(id)sender
{
    [self.people setValue:self.nameTextField.text forKey:@"name"];
    [self.people setValue:[NSNumber numberWithInt:[self.numberTextField.text intValue]] forKey:@"number"];
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"updated" message:@"record updated" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {      
        [self.navigationController popViewControllerAnimated:YES];
    }
}


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

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.nameTextField resignFirstResponder];
    [self.numberTextField resignFirstResponder];
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.numberTextField.delegate=self;
    self.nameTextField.delegate=self;

    self.title=self.people.name;
    self.nameTextField.text=self.people.name;
    self.numberTextField.text=self.people.number.stringValue;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值