传递的数组不是由 popViewControllerAnimated 传递的...为什么?
不要被这个巨大的问题吓倒......(主要是代码)。
好的,我有一个导航控制器,其中包含一个包含 tableView 的视图控制器(称为 AddClaim)。 如果选择了一个单元格,则称为:
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 ];
这工作得很好,并且显示一个包含 tableView 的新视图控制器(它是一个独占列表)。
在 EditClaimDetails 的 ViewDidLoad 中存在此代码:(claimTypeHoldingArray 是在头文件中声明的可变数组)
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(pressedBack)];
self.navigationItem.leftBarButtonItem = backButton;
claimTypeHoldingArray = [[NSMutableArray alloc] initWithArray:newClaimArrayDetails2];
基本上,其结果如预期:显示后退按钮 - 按下时 - 它调用选择器将视图控制器弹出到 AddClaim, ClaimTypeHoldingArray 包含 AddClaim 中给出的 newClaimsArray。
这是 didSelectRowAtIndexPath 中代码的一部分:(claimTypeArray 是保存单元格文本标签的数组)
[claimTypeHoldingArray replaceObjectAtIndex:0 withObject:[claimTypeArray objectAtIndex:indexPath.row]];
它的作用是用单元格 TextLabel 上的文本替换 ClaimTypeHoldingArray 的第一个对象。到目前为止,一切都很好。 (用 nslog 测试)
这是按下后退按钮时的代码:
-(IBAction)pressedBack {
AddClaim *sender = [[[AddClaim alloc] init] autorelease];
sender.newClaimArrayDetails = claimTypeHoldingArray;
[self.navigationController popViewControllerAnimated:YES];
这就是麻烦开始的地方...... 此操作(根据我的说法)应将 newClaimArrayDetails 替换为 ClaimTypeHoldingArray。 (确实如此)但是当视图控制器弹出并且屏幕向后移动以添加声明时,该数组不会更改! 我做错了什么?!顺便说一句,所有属性都已设置。 这是我在 viewDidAppear 中所做的测试:
NSLog(@"%@",[newClaimArrayDetails objectAtIndex:0]);
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 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]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个答案与问题的规模相同,希望它不会太大;)
因此,在按下Back按钮的方法中,您尝试使用claimTypeHoldingArray更新初始的AddClaim视图控制器对象。
你说对了一半 - 你肯定是在更新 AddClaim 对象,而不是导航控制器内的对象。您正在创建一个新的并更新它!
您需要将创建它的 AddClaim 视图控制器传递到 EditClaimDetails 视图控制器中。
在您的单元格中选择方法添加类似的内容
(其中addClaimViewCOntroller是您的EditClaimDetails对象上的一个属性,例如
然后,您的pressedBack方法变为
希望有帮助。
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!
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
(where addClaimViewCOntroller is a property on your EditClaimDetails object like
Then, your pressedBack method becomes
Hope that helps.
检查 AddClaim 中的数组属性定义,是否有可能(非原子、复制)?如果是,它将保存您的数组的私有副本,以便您的原始数组无法更改。
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.