在 2 个控制器之间共享数组中的 didSelectRowAtIndexPath 选择

发布于 2024-08-15 21:21:04 字数 4409 浏览 5 评论 0原文

在我永无止境地寻求将表行选择共享到模式视图中加载的多个视图时,我将其归结为一个问题。我使用 Apples DrillDownSave 示例代码作为模型来重现这种魔力。并且认为我将所有设置都完全相同...

在 RootViewController.m 的 didSelectRowAtIndexPath 部分中,我使用以下代码将 violinMakers MSMutableArray 复制到同名的 DetailsView MSMuteableArray。所有颜色编码,没有错误告诉我我走在正确的道路上..但是..

detailsView.violinMakers = [violinMakers objectAtIndex:indexPath.row];

在 RootviewController.h 文件中,我通过以下方式引用了DetailsViewController:

@class DetailViewController

DetailsViewController *detailsView;
NSMutableArray *violinMakers; 
@property (nonatomic, retain) NSMutableArray *violinMakers;

然后在DetailsViewController .h 文件中:

NSMutableArray *violinMakers;
IBOutlet UITextView *violinMakerDescription;
@property (nonatomic, retain) NSMutableArray *violinMakers; //tried assign or copy no difference
@property (nonatomic, retain) IBOutlet UITextView *violinMakerDescription;

在DetailsViewController .m 文件中:

- (void)viewWillAppear:(BOOL)animated
ViolinMaker *violinMaker = (ViolinMaker *)violinMakers;
[violinMakerDescription setText: [violinMaker description]];
 NSLog(@"violinMakerDescription in detailsView: %@", [violinMaker description]);

我有一个单独的类,如上所示,我用它来定义数据 violinMaker。这在 didSelectRowAtIndexPath 内的 rootviewcontroller 中工作正常,所以我知道这不是问题的一部分,我当然在 .m 文件中引用它。 它像平常一样在 IB 中连接起来,因为我测试它的工作原理是通过简单地从 rootViewController 直接推送 DetailsViewController,并且数据可以很好地加载到 UITextView 中。

我从 RootviewController (tableViewColler) 的推送视图(工作完美)启动DetailsViewController,它作为模态视图启动:

DetailsViewController *detailsViewController = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:nil ];
UINavigationController *aDetailsViewController = [[UINavigationController alloc] initWithRootViewController:detailsViewController];

[self presentModalViewController:aDetailsViewController animated:YES];

// [violinMakerView release];
[DetailsViewController retain]; // or release no different
[aDetailsViewController release];

我在构建和视图加载时没有收到错误,但文本视图中的信息为空白。这也告诉我它没有加载任何内容,并且连接正确...我非常接近...但是我可以从我的 NSLog 中看到该值是:

violinMakerDescription in detailsView: (null)

所以 RootviewController 中的 violinMakers 没有被复制到 violinMakers 数组中详细信息视图控制器。我不愿意承认这一点,但几个星期以来,我尝试了各种解决方案,通过 appDelegate、NSDictionary 和各种其他方式使其正常工作,但均无济于事。我认为答案是 violinMakers 被发布,然后什么都不传递,或者 RootViewController 中的分配行不起作用。有人知道我可以改变什么吗?

我真的很感激一些帮助,我只是没有得到我所缺少的东西,但这是我得到的最接近的我只需要正确的链接,或者看看为什么这些值没有被保留。如果有办法检查值何时发布,我可以检查这个问题,也许就是这样。我试图在任何地方保留它(并在我让它工作后纠正),但它就是不起作用......帮助?

谢谢大家!!!

真诚的柯克

尝试将 RootViewController 中的加载行更改为:

[detailsView.violinMakers addObject: [violinMakers objectAtIndex:indexPath.row]];

@Frank 首先感谢您对此的热情帮助! 我确实以相同的方式在 rootViewController 中设置了 violinMaker 对象,并且它在那里工作得很好,如下所示:

violinMaker = [self.violinMakers objectAtIndex:indexPath.row];

        ViolinMakerViewController *violinMakerViewController = [[ViolinMakerViewController alloc] initWithNibName:@"ViolinMakerViewController" bundle:nil];
        self.violinMakerView = violinMakerViewController;

        [[self navigationController] pushViewController:violinMakerView animated:YES];


        [self.violinMakerView.violinMakerName setText:[violinMaker name]];
    ....
        [self.violinMakerView.violinMakerDescription setText:[violinMaker description]];

        NSLog(@"violinMaker description variable in RootView: %@", [violinMaker description]);

        DetailsViewController.violinMakers = [violinMakers objectAtIndex:indexPath.row];

这里我在 NSLog 中看到它在这里加载得很好,所以我相信将它从 objectAtIndex:indexPath.row 加载到数组中,然后复制该数组并将其从 DetaisViewController 放入 ViolinMaker 对象中就可以了。我不想在 DetailsViewController 中再次重新创建 ViolinMaker 对象,只需从数组中重新加载它,就像从 RootViewController 中一样。

这就是 DrillDownSave Apple 示例的做法,我已经镜像了此配置。在 didSelectRowAtIndex 中的 level1ViewController 中,以下代码

// provide the 2nd level its content
level2ViewController.listContent = [[listContent objectAtIndex:indexPath.row] objectForKey:kChildrenKey];

我不需要关键部分,因为那是用于调用位置的。示例代码中的两个控制器都使用同名的 NSArray,并且似乎是通过此行将 L1 数组复制到 L2 数组。

他们使用推送的 TableViews,这会变得很容易,但我不只是这样做,IBOutlets 将信息加载到 UITextView,所以我必须在发生选择的 RootView 控制器中进行数组的分配。 如果我可以保留在 RootView 控制器中分配的 ViolinMaker,那么我就可以在“详细信息”中选择它,但我似乎也无法弄清楚......

谢谢你帮助我,我真的被困住了......

On my never ending quest to share a table row selection to multiple views loaded in a modal view, I have got it down to one issue. I am using Apples DrillDownSave sample code as a model to recreate this magic. And think I have it all set up exactly the same...

In the didSelectRowAtIndexPath section of my RootViewController.m, I have the following code to copy violinMakers MSMutableArray to the DetailsView MSMuteableArray of the same name. All colour coding, and no errors tells me i am on the right path.. but..

detailsView.violinMakers = [violinMakers objectAtIndex:indexPath.row];

in the RootviewController.h file I reference the DetailsViewController through:

@class DetailViewController

DetailsViewController *detailsView;
NSMutableArray *violinMakers; 
@property (nonatomic, retain) NSMutableArray *violinMakers;

Then in DetailsViewController .h file:

NSMutableArray *violinMakers;
IBOutlet UITextView *violinMakerDescription;
@property (nonatomic, retain) NSMutableArray *violinMakers; //tried assign or copy no difference
@property (nonatomic, retain) IBOutlet UITextView *violinMakerDescription;

And in the DetailsViewController .m file:

- (void)viewWillAppear:(BOOL)animated
ViolinMaker *violinMaker = (ViolinMaker *)violinMakers;
[violinMakerDescription setText: [violinMaker description]];
 NSLog(@"violinMakerDescription in detailsView: %@", [violinMaker description]);

I have a separate class, as seen above that I use to define the data violinMaker. This works fine in the rootviewcontroller inside the didSelectRowAtIndexPath , so I know that is not part of the problem, i reference it of course in the .m file.
It is all hooked up in IB as normal, as I test that it works by simply pushing the DetailsViewController Directly from the rootViewController, and the data loads fine into the UITextView.

I am launching the DetailsViewController from a pushed view (works perfect) from the RootviewController (tableViewColler) and it is launching as a Modal view by:

DetailsViewController *detailsViewController = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:nil ];
UINavigationController *aDetailsViewController = [[UINavigationController alloc] initWithRootViewController:detailsViewController];

[self presentModalViewController:aDetailsViewController animated:YES];

// [violinMakerView release];
[DetailsViewController retain]; // or release no different
[aDetailsViewController release];

I get no errors on build, and the View Loads, but the info is blank in the textView. This also tells me it is loading nothing, and connected properly... I am so close... But I can see from my NSLog that that the value is:

violinMakerDescription in detailsView: (null)

so the violinMakers from the RootviewController is not being copied into the violinMakers array in the DetailsViewController. I hate to admit it, but for weeks I have tried various solutions to get this to work though appDelegate, through NSDictionary, and all kinds of other ways to no avail. I think the answer is with the violinMakers being released so then just passing on nothing, or the assigning line in the RootViewController is not working. Anyone know what I can change?

I really would appreciate some help, I am just not getting what I am missing, but this is the closest I have gotten I just need the link right, or see why the values are not being retained. If there is a way to check the when the values are released, I could check for this issue, maybe that is it. I have tried to retain it everywhere (and correct after I get it to work) but it just is not working... help??

Thanks everyone!!!

Sincerely Kirk

Tried changing the loading line in the RootViewController to:

[detailsView.violinMakers addObject: [violinMakers objectAtIndex:indexPath.row]];

@Frank
Firstly thanks for your kind help on this!!
I do set the violinMaker object in the rootViewController the same way, and it works fine there, as below:

violinMaker = [self.violinMakers objectAtIndex:indexPath.row];

        ViolinMakerViewController *violinMakerViewController = [[ViolinMakerViewController alloc] initWithNibName:@"ViolinMakerViewController" bundle:nil];
        self.violinMakerView = violinMakerViewController;

        [[self navigationController] pushViewController:violinMakerView animated:YES];


        [self.violinMakerView.violinMakerName setText:[violinMaker name]];
    ....
        [self.violinMakerView.violinMakerDescription setText:[violinMaker description]];

        NSLog(@"violinMaker description variable in RootView: %@", [violinMaker description]);

        DetailsViewController.violinMakers = [violinMakers objectAtIndex:indexPath.row];

Here I see in the NSLog it loads fine here, so I believed loading it here into the array from the objectAtIndex:indexPath.row then copying that array across and putting it into the ViolinMaker object from the DetaisViewController would work. I didn't want to have to recreate the ViolinMaker object again inside the DetailsViewController, just reload it from the array as it had been from the RootViewController.

This is how the DrillDownSave Apple example does it, and I have Mirrored this configuration. Where in the level1ViewController in the didSelectRowAtIndex the following code

// provide the 2nd level its content
level2ViewController.listContent = [[listContent objectAtIndex:indexPath.row] objectForKey:kChildrenKey];

I don't need the key part, as that is for recalling location. Both controllers in there sample code use NSArrays of the same name and it appears to be copying the L1 array to the L2 array through this line.

They use pushed TableViews, which would make it easy, but I am not doing that just, IBOutlets to load the information to UITextView's, so I have to do the assignment of the array in the RootView Controller, where the selection happens.
If I could just retain the ViolinMaker assigned in the RootView Controller, I could then just pick it up in the Details, but I can't seem to figure that out either....

Thanks for helping me, I am really stuck..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

随心而道 2024-08-22 21:21:04

我认为问题的关键在于这一行:

ViolinMaker *violinMaker = (ViolinMaker *)violinMakers;

您将 NSMutableArray 对象的地址分配给 violinMaker。如果我理解正确的话,violinMakers 是一个ViolinMaker 对象的数组。在这种情况下,您可能需要:

ViolinMaker *violinMaker = [violinMakers objectAtIndex:index];

您必须在 didSelectRowAtIndexPath: 过程中获取索引。事实上,更好的方法可能如下:

  1. 将您的DetailsViewController更改为具有单个violinMaker属性(而不是数组)
  2. 分配DetailsViewController后,设置violinMaker属性:

    detailsViewController.violinMaker = [violinMakers objectAtIndex:indexPath.row];

然后删除演员表(第一个代码示例),您应该就在那里。

不幸的是,Objective-C/XCode 并不像 VS200x/.NET 那样适合“通过 IntelliSense 进行编码”。学习几个教程然后再回到这个项目可能是明智的做法。

I think the crux of the problem is this line:

ViolinMaker *violinMaker = (ViolinMaker *)violinMakers;

You're assigning the address of the NSMutableArray object to violinMaker. If I'm understanding correctly, violinMakers is an array of ViolinMaker objects. In that case you would want:

ViolinMaker *violinMaker = [violinMakers objectAtIndex:index];

You'll have to grab the index in the didSelectRowAtIndexPath: procedure. In fact a better way to do it might be as follows:

  1. Change your DetailsViewController to have a single violinMaker property (not an array)
  2. After you allocate your DetailsViewController, set the violinMaker property:

    detailsViewController.violinMaker = [violinMakers objectAtIndex:indexPath.row];

Then remove the cast (first code sample) and you should be mostly there.

Unfortunately Objective-C/XCode doesn't lend itself to "coding by IntelliSense" as well as VS200x/.NET does. It would probably be wise to work through a couple of tutorials and then come back to this project.

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