传递的数组不是由 popViewControllerAnimated 传递的...为什么?

发布于 2024-11-04 10:28:43 字数 1872 浏览 0 评论 0原文

不要被这个巨大的问题吓倒......(主要是代码)。
好的,我有一个导航控制器,其中包含一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐偏执 2024-11-11 10:28:43

这个答案与问题的规模相同,希望它不会太大;)

因此,在按下Back按钮的方法中,您尝试使用claimTypeHoldingArray更新初始的AddClaim视图控制器对象。

你说对了一半 - 你肯定是在更新 AddClaim 对象,而不是导航控制器内的对象。您正在创建一个新的并更新它!

-(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];

您需要将创建它的 AddClaim 视图控制器传递到 EditClaimDetails 视图控制器中。

在您的单元格中选择方法添加类似的内容

detailViewController.addClaimViewController = self;

(其中addClaimViewCOntroller是您的EditClaimDetails对象上的一个属性,例如

@property (nonatomic, retain) Addclaim *addClaimViewController;

然后,您的pressedBack方法变为

-(IBAction)pressedBack {
    // This line updates your _old_ AddClaim view controller
    addClaimViewController.newClaimArrayDetails = claimTypeHoldingArray;

    // This line displays the _old_ AddClaim object
    [self.navigationController popViewControllerAnimated:YES];

希望有帮助。

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.

神回复 2024-11-11 10:28:43

检查 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文