- (id)initWithManagedObjectContext:(NSManagedObjectContext *)moc
{
   if ((self = [super initWithNibName:@"PersonDetailViewController" bundle:nil])) {
      managedObjectContext = [moc retain];
   }
   return self;
}

- (void)saveAndDismiss
{
   if (!person) {
      person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" 
                  inManagedObjectContext:managedObjectContext];
   }

   if ([[firstNameTextField text] length] < 1 || 
         [[lastNameTextField text] length] < 1) {
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Błąd"
            message:@"Podanie imienia i nazwiska jest obowiązkowe."
            delegate:nil
            ancelButtonTitle:nil
            otherButtonTitles:@"OK", nil];
      [alert show];
      [alert release];
   } else {
      [person setValue:[firstNameTextField text] forKey:@"firstName"];
      [person setValue:[lastNameTextField text] forKey:@"lastName"];

      NSError *error = nil;
      if (![managedObjectContext save:&error]) {
         NSLog(@"Wystąpił błąd %@, %@", error, [error userInfo]);
      } else {
         [self dismissModalViewControllerAnimated:YES];
      }
   }
}
