我有一个表视图,其中包含用户可以更新的不同信息。其中一个是"试点信息"。当用户点击此行时,他们将转到另一个视图控制器,在那里他们填写有关飞行员的信息。 (姓名电话号码等) 然后,他们可以点击保存或取消两者,将它们返回到上一个表视图控制器。我想要做的是让用户能够再次点击导频信息,并查看他们刚输入的数据并在他们选择时进行编辑。我的解决方案是添加另一个segue并使用以下代码为其提供唯一标识符。 (pilot是我核心数据模型中实体的名称,updatepilot是我给segue的名字。)
- (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;
}
}
!http://tinypic.com/r/2ihbera/8
但是,我得到了未声明标识符' destviewcontroller"的错误使用。你的意思是UIViewController?我有没有看到这里的东西?任何帮助将不胜感激!
#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 :(得分:0)
尝试将segue.destinationViewController
(类型id
)强制转换为PilotViewController。
PilotViewController *destViewController = (PilotViewController *)segue.destinationViewController;
在Xcode的早期版本中,我相信我认为这是一个警告;您的构建设置可能设置为将所有警告视为错误。