Passed array is not passed by popViewControllerAnimated... why?
Dont be put off by the huge question... (its mostly code).
OK, I have a navigation Controller which contains a view controller (Called AddClaim) containing a tableView. if a cell is selected, this is called:EditClaimDetails *detailViewController = [[[EditClaimDetails alloc] init] autorelease];
// Pass the selected object to the new view controller.
detailViewController.selectedIndexPath = indexPath;
detailViewController.newClaimArrayDetails2 = newClaimArrayDetails;
[self.navigationController pushViewController:detailViewController animated:YES ];
This works nicely and a new view controller is shown containing a tableView (It is an exclusive list).
In ViewDidLoad of the EditClaimDetails this code exists: (claimTypeHoldingArray is a mutable array declared in the header file)
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(pressedBack)];
self.navigationItem.leftBarButtonItem = backButton;
claimTypeHoldingArray = [[NSMutableArray alloc] initWithArray:newClaimArrayDetails2];
Basically the result of this is as expected: A back button is shown - when pressed - it calls a selector popping the view controller to AddClaim, claimTypeHoldingArray contains the newClaimsArray given in AddClaim.
This is part of the code in didSelectRowAtIndexPath: (claimTypeArray is the array which holds the textLabels of the cells)
[claimTypeHoldingArray replaceObjectAtIndex:0 withObject:[claimTypeArray objectAtIndex:indexPath.row]];
What this does is that the first object of claimTypeHoldingArray is replaced with what text was on 开发者_C百科the TextLabel of the cell. so far so good. (tested with nslog)
This is the code for when the back button is pressed:
-(IBAction)pressedBack {
AddClaim *sender = [[[AddClaim alloc] init] autorelease];
sender.newClaimArrayDetails = claimTypeHoldingArray;
[self.navigationController popViewControllerAnimated:YES];
This is where the trouble starts... This action (according to me) should replace newClaimArrayDetails with claimTypeHoldingArray. (it does so) But when the view controller is popped and the screen moves back to add claim this array is not changed! What have I done wrong?! btw, all properties are set. this is the test i do in viewDidAppear:
NSLog(@"%@",[newClaimArrayDetails objectAtIndex:0]);
This answer is the same scale as the question, hope its's not too large ;)
So, in your pressedBack button method you're trying to update the initial AddClaim view controller object with the claimTypeHoldingArray.
You're halfway right - you're definitely updating an AddClaim object, just not the one that's inside your navigation controller. you're creating a new one and updating that instead!
-(IBAction)pressedBack {
// This line creates a new AddClaim view controller
AddClaim *sender = [[[AddClaim alloc] init] autorelease];
// This line updates your _new_ AddClaim view controller
sender.newClaimArrayDetails = claimTypeHoldingArray;
// This line displays the _old_ AddClaim object
[self.navigationController popViewControllerAnimated:YES];
You need to pass into your EditClaimDetails view controller the AddClaim view controller that created it.
In your cell is selected method add something like
detailViewController.addClaimViewController = self;
(where addClaimViewCOntroller is a property on your EditClaimDetails object like
@property (nonatomic, retain) Addclaim *addClaimViewController;
Then, your pressedBack method becomes
-(IBAction)pressedBack {
// This line updates your _old_ AddClaim view controller
addClaimViewController.newClaimArrayDetails = claimTypeHoldingArray;
// This line displays the _old_ AddClaim object
[self.navigationController popViewControllerAnimated:YES];
Hope that helps.
Check your array property definition in AddClaim, is it by any chance (nonatomic, copy)? If yes the it would hold a private copy of your array, so that you original array couldn't change.
精彩评论