开发者

Share didSelectRowAtIndexPath selection in an array, between 2 controllers

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, wher开发者_JS百科e 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..


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜