通过segue推送数据而不删除以前的数据

时间:2014-12-31 06:06:39

标签: ios objective-c iphone xcode

我有一个表视图控制器,其中包含使用核心数据保存的文本字段。我还有一个搜索显示视图控制器,我使用push segue将它们链接在一起,并使用Prepareforsegue的另一个推送将信息链接回同一个表视图控制器。但我的问题是信息没有保存回原始的表视图控制器,但每次调用准备segue时都会创建一个新信息。


这是表视图控制器代码 -

 //

//  PersonDetailTVC.m

//  Staff Manager

//

//  Created by Tim Roadley on 14/02/12.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//



#import "PersonDetailTVC.h"

#import "PersonsTVC.h"

#import <LibXL/LibXL.h>



@implementation PersonDetailTVC{



    NSArray *recipes;

    NSArray *searchResults;

 UIImage *image;

    UIImage *imagetwo;

    UIImage *imagethree;

    UIImage *imagefour;

    UIImage *imagefive;

    UIImage *imagesix;

    UIImage *imageseven;

    UIImage *imageeight;

    UIImage *imagenine;

    UIImage *imageten;

}



@synthesize delegate;

@synthesize person = _person;



@synthesize selectedRole;



@synthesize recipeLabel;

@synthesize recipeName;

@synthesize LinkLabel;







@synthesize personroomTextField = _personroomTextField;

@synthesize personFirstnameTextField = _personFirstnameTextField;

@synthesize personaddressTextField = _personaddressTextField;



@synthesize personcityTextField = _personcityTextField;





@synthesize personstateTextField = _personstateTextField;

@synthesize personstateTextField1 = _personstateTextField1;



@synthesize personzipTextField = _personzipTextField;





@synthesize personinsuranceTextField = _personinsuranceTextField;



@synthesize personclaimTextField = _personclaimTextField;



@synthesize persontaxTextField = _persontaxTextField;



@synthesize personRoleTableViewCell = _personRoleTableViewCell;





@synthesize personquantityTextField = _personquantityTextField;





@synthesize personageTextField = _personageTextField;





@synthesize persondescTextField = _persondescTextField;



@synthesize personserialTextField = _personserialTextField;



@synthesize personpriceTextField = _personpriceTextField;



@synthesize personnotesTextField = _personnotesTextField;



@synthesize personrcvperTextField = _personrcvperTextField;



@synthesize persontotaldepTextField = _persontotaldepTextField;



@synthesize personacvTextField = _personacvTextField;



@synthesize personconditionTextField = _personconditionTextField;



@synthesize personstoredImage = _personstoredImage;



@synthesize personimgThumbNail = _personimgThumbNail;

- (void)viewDidLoad

{

    NSLog(@"Setting the value of fields in this static table to that of the passed Person");



    self.personFirstnameTextField.text = self.person.firstname;

    self.personaddressTextField.text = self.person.address;

    self.personcityTextField.text = self.person.city;

    self.personstateTextField.text = self.person.state;

    self.personzipTextField.text = self.person.zip;

    self.personinsuranceTextField.text = self.person.insurance;

    self.personclaimTextField.text = self.person.claim;

    self.persontaxTextField.text = self.person.tax;

    self.personquantityTextField.text = self.person.quantity;

    self.personRoleTableViewCell.textLabel.text = self.person.inRole.name;

    self.personRoleTableViewCelltwo.textLabel.text = self.person.inRole.name;

    self.personquantityTextField.text = self.person.quantity;

    self.personconditionTextField.text = self.person.condition;

    self.personageTextField.text = self.person.age;

    self.persondescTextField.text = self.person.desc;

    self.personserialTextField.text = self.person.serial;

    self.personnotesTextField.text = self.person.notes;

    self.personpriceTextField.text = self.person.price;

    self.personrcvperTextField.text = self.person.rcvper;

    self.personacvTextField.text = self.person.acv;

    self.persontotaldepTextField.text = self.person.totaldep;

    self.selectedRole = self.person.inRole; // ensure null role doesn't get saved.

    self.selectedRoletwo = self.person.inRole; // ensure null role doesn't get saved.



    self.personroomTextField.text = self.person.room;

    self.personroomTextField1.text = self.person.room1;

    self.personroomTextField2.text = self.person.room2;

    self.personroomTextField3.text = self.person.room3;

    self.personroomTextField4.text = self.person.room4;

    self.personroomTextField5.text = self.person.room5;

    self.personroomTextField6.text = self.person.room6;

    self.personroomTextField7.text = self.person.room7;

    self.personroomTextField8.text = self.person.room8;

    self.personroomTextField9.text = self.person.room9;

    self.personroomTextField10.text = self.person.room10;







    self.title = self.recipe.name;

    self.prepTimeLabel.text = self.recipe.prepTime;

    self.recipeNameLabel.text = self.recipe.name;

    self.LinkLabel.text = self.recipe.Link;



    self.prepTimeLabel1.text = self.recipe.prepTime1;

    self.recipeNameLabel1.text = self.recipe.name;

    self.LinkLabel1.text = self.recipe.Link1;



    self.persontotaldepTextField50.text = self.person.totaldep50;

    self.selectedRole = self.person.inRole; // ensure null role doesn't get saved.



     [self.tableView reloadData];

    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];

    [tgr setCancelsTouchesInView:NO];

    [self.tableView addGestureRecognizer:tgr];











        [super viewDidLoad];







}





- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        PersonDetailTVC *destViewController = segue.destinationViewController;

        destViewController.person = [recipes objectAtIndex:indexPath.row];





    }

}







- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {



    if (indexPath.section == 0) {

        switch (indexPath.row) {

            case 0:

                [self.personFirstnameTextField becomeFirstResponder];

                break;

            case 1:

                [self.personaddressTextField becomeFirstResponder];

            case 2:

                [self.personcityTextField becomeFirstResponder];

            case 3:

                [self.personstateTextField becomeFirstResponder];

            case 4:

                [self.personzipTextField becomeFirstResponder];

            case 6:

                [self.personinsuranceTextField becomeFirstResponder];

            case 7:

                [self.personclaimTextField becomeFirstResponder];

            case 8:

                [self.persontaxTextField becomeFirstResponder];

            case 9:

                [self.personquantityTextField becomeFirstResponder];

            case 10:

                [self.personconditionTextField becomeFirstResponder];

            case 11:

                [self.personageTextField becomeFirstResponder];

            case 12:

                [self.persondescTextField becomeFirstResponder];

            case 13:

                [self.personserialTextField becomeFirstResponder];

            case 14:

                [self.personpriceTextField becomeFirstResponder];

            case 15:

                [self.personnotesTextField becomeFirstResponder];

            case 16:

                [self.personrcvperTextField becomeFirstResponder];

            case 17:

                [self.personacvTextField becomeFirstResponder];

            case 18:

                [self.persontotaldepTextField becomeFirstResponder];

            case 19:

                [self.personstoredImage becomeFirstResponder];





            default:

                break;

        }

    }

}



- (void)viewDidUnload

{

    //[self setPersonNameTextField:nil];

    [self setPersonFirstnameTextField:nil];

    [self setPersonaddressTextField:nil];

    [self setPersoncityTextField:nil];

    [self setPersonstateTextField:nil];

    [self setPersonzipTextField:nil];

    [self setPersoninsuranceTextField:nil];

    [self setPersonclaimTextField:nil];

    [self setPersontaxTextField:nil];

    [self setPersonquantityTextField:nil];

    [self setPersonRoleTableViewCell:nil];

    [self setPersonRoleTableViewCelltwo:nil];

    [self setPersonconditionTextField:nil];

    [self setPersonageTextField:nil];

    [self setPersonserialTextField:nil];

    [self setPersondescTextField:nil];

    [self setPersonpriceTextField:nil];

    [self setPersonnotesTextField:nil];

    [self setPersonrcvperTextField:nil];

    [self setPersontotaldepTextField:nil];

    [self setPersonacvTextField:nil];



    [super viewDidUnload];

    NSString *fullURL = @"http://conecode.com";

    NSURL *url = [NSURL URLWithString:fullURL];

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [_viewWeb loadRequest:requestObj];

}



- (IBAction)save:(id)sender

{

    NSLog(@"Telling the PersonDetailTVC Delegate that Save was tapped on the PersonDetailTVC");



    self.person.firstname = self.personFirstnameTextField.text; // Set Firstname

    self.person.address = self.personaddressTextField.text; // Set Surname

    self.person.city = self.personcityTextField.text;

    self.person.state = self.personstateTextField.text;

    self.person.zip = self.personzipTextField.text;

    self.person.insurance = self.personinsuranceTextField.text;

    self.person.claim = self.personclaimTextField.text;

    self.person.tax = self.persontaxTextField.text;

    self.person.quantity = self.personquantityTextField.text;

    self.person.condition = self.personconditionTextField.text;

    self.person.age = self.personageTextField.text;

    self.person.desc = self.persondescTextField.text;

    self.person.serial = self.personserialTextField.text;

    self.person.price = self.personpriceTextField.text;

    self.person.notes = self.personnotesTextField.text;

    self.person.rcvper = self.personrcvperTextField.text;

    self.person.acv = self.personacvTextField.text;

    self.person.totaldep = self.persontotaldepTextField.text;





    [self.persontwo setInRole:self.selectedRole]; // Set Relationship!!!

    [self.persontwo.managedObjectContext save:nil];  // write to database

    [self.delegate personDetailTVCDidSave:self];

    [self.person setInRole:self.selectedRole]; // Set Relationship!!!

    [self.person setInRole:self.selectedRoletwo];

    [self.person.managedObjectContext save:nil];  // write to database

    [self.delegate personDetailTVCDidSave:self];

}







- (void)dismissKeyboard {

    [self.view endEditing:TRUE];

}



- (void)roleWasSelectedOnPersonRoleTVC:(PersonRoleTVC *)controller

{

    self.personRoleTableViewCell.textLabel.text = controller.selectedRole.name;

    self.selectedRole = controller.selectedRole;

    NSLog(@"PersonDetailTVC reports that the %@ role was selected on the PersonRoleTVC", controller.selectedRole.name);

    [self.person setInRole:self.selectedRole];

    [self.person.managedObjectContext save:nil];

    [delegate personDetailTVCDidSave:self];

    [controller.navigationController popViewControllerAnimated:YES];



    self.personRoleTableViewCelltwo.textLabel.text = controller.selectedRole.name;

    self.selectedRoletwo = controller.selectedRole;

    NSLog(@"PersonDetailTVC reports that the %@ role was selected on the PersonRoleTVC", controller.selectedRole.name);

    [self.person setInRole:self.selectedRoletwo];

    [self.person.managedObjectContext save:nil];

    [delegate personDetailTVCDidSave:self];

    [controller.navigationController popViewControllerAnimated:YES];

}



- (void)personChangedOnMaster:(PersonsTVC *)controller {



    self.person = controller.selectedPerson;

    NSLog(@"PersonDetailTVC.m: personChangedOnMaster: %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@",self.person.firstname, self.person.address, self.person.city, self.person.state, self.person.zip, self.person.insurance, self.person.claim, self.person.tax, self.person.quantity, self.person.condition, self.person.age, self.person.desc, self.person.serial, self.person.price, self.person.notes, self.person.rcvper, self.person.totaldep,



          self.person.serial1, self.person.price1, self.person.notes1, self.person.rcvper1, self.person.totaldep1,



          self.person.serial2, self.person.price2, self.person.notes2, self.person.rcvper2, self.person.totaldep2,



          self.person.serial3, self.person.price3, self.person.notes3, self.person.rcvper3, self.person.totaldep3,



          self.person.serial4, self.person.price4, self.person.notes4, self.person.rcvper4, self.person.totaldep4,



          self.person.serial5, self.person.price5, self.person.notes5, self.person.rcvper5, self.person.totaldep5,



          self.person.serial6, self.person.price6, self.person.notes6, self.person.rcvper6, self.person.totaldep6,



          self.person.serial7, self.person.price7, self.person.notes7, self.person.rcvper7, self.person.totaldep7,



          self.person.serial8, self.person.price8, self.person.notes8, self.person.rcvper8, self.person.totaldep8,



          self.person.serial9, self.person.price9, self.person.notes9, self.person.rcvper9, self.person.totaldep9,



          self.person.serial10, self.person.price10, self.person.notes10, self.person.rcvper10, self.person.totaldep10,







          self.person.room,

          self.person.room1,

          self.person.room2,

          self.person.room3,

          self.person.room4,

          self.person.room5,

          self.person.room6,

          self.person.room7,

          self.person.room8,

          self.person.room9,

          self.person.room10,



          self.person.acv, self.person.storedImage);

    [self.navigationController popViewControllerAnimated:YES]; // Return detail view to root.

    [self viewDidLoad];



    self.persontwo = controller.selectedPerson;



    NSLog(@"PersonDetailTVC.m: personChangedOnMaster: %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ ",self.person.firstname, self.person.address, self.person.city, self.person.state, self.person.zip, self.person.insurance, self.person.claim, self.person.tax, self.person.quantity, self.person.condition, self.person.age, self.person.desc, self.person.serial, self.person.price, self.person.notes, self.person.rcvper, self.person.totaldep, self.person.acv, self.person.storedImage);

    [self.navigationController popViewControllerAnimated:YES]; // Return detail view to root.

    [self viewDidLoad];





}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

    } else {

        return YES;

    }

}





@end

这是搜索显示控制器代码 -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
        NSIndexPath *indexPath = nil;
        Recipe *recipe = nil;
        if (self.searchDisplayController.active) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            recipe = [searchResults objectAtIndex:indexPath.row];
        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            recipe = [recipes objectAtIndex:indexPath.row];
        }


PersonDetailTVC *destViewController = segue.destinationViewController;
        destViewController.recipe = recipe;

        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

1 个答案:

答案 0 :(得分:0)

无需通过segue传递数据,因为当segue执行时总是创建新实例。 在PersonDetailTVC.h中添加此内容

   - (IBAction)backFromMapView:(UIStoryboardSegue *)segue
{
    NSLog(@"and we are back");
    PersonDetailTVC* person = (PersonDetailTVC *)segue.sourceViewController;
    NSLog(@"location %@", person.recipe);
}

帮助您解决segue的表现。 这个链接有助于如何更清楚地说明:

IBOutlet does not display its value in UI