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