views:

44

answers:

3

Hi All,

I am loading new views for a small iphone app, and was wondering how to pass details from one to another? I am loading a tableview full of data from and xml file, then once clicked a new view is brought in via:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    SubInfoViewController *subcontroller = [[SubInfoViewController alloc] initWithNibName:@"SubInfoView" bundle:nil];
    [self presentModalViewController:subcontroller animated:YES];
    [subcontroller release];

}

Next step would be to tell the newly loaded view which row had just been loaded? Any idea, thoughts more than welcome, and please be gentle big newbie...

A: 

Add an instance variable to your view controller and declare a property corresponding to it, so after you alloc, init it, set it like subcontroller.foo = Blah Blah.

So something like:SubInfoViewController *subcontroller = [[SubInfoViewController alloc] initWithNibName:@"SubInfoView" bundle:nil];subcontroller.clickedPath = indexPath;[self presentModalViewController:subcontroller animated:YES];[subcontroller release];
jimbo
+3  A: 

I typically create my own init method to do things like this. I think it would likely be better to pass in the corresponding "model" object represented by the tableView row, rather than the row number itself, like this:

In SubInfoViewController.h

@interface SubInfoViewController : UIViewController {
    YourObject *yourObject;
}

@property (nonatomic, retain) YourObject *yourObject;

Then in SubInfoViewController.m:

- (SubInfoViewController*)initWithYourObject:(YourObject*)anObject {
   if((self = [super initWithNibName@"SubInfoView" bundle:nil])) {
       self.yourObject = anObject;
   }
   return self;
}

You'd create and present it this way:

// assuming you've got an array storing objects represented 
// in the tableView called objectArray

SubInfoViewController *vc = [[SubInfoViewController alloc] initWithYourObject:[objectArray objectAtIndex:indexPath.row]];
[self presentModalViewController:vc animated:YES];
[vc release];

This could be adapted pretty easily to allow you to pass in any type of object or value (such as a row number if you still want to do that).

Steve N
Hi, I think i almost understand this ;) would the last bit of code go within '- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {' in InfoViewController.m?
jimbo
Yes- that's right. That method will provide the selected row, which you use to index into the array containing the objects represented in your tableView. You can then grab and pass that object to the new view controller you are creating.
Steve N
Hi Steve, getting a funny error... expected specifier-qualifier-list before 'YourObject' (sorry if this newbie stuff...slowly getting there)
jimbo
Make sure you replace references in my example code to "YourObject" with whatever you have called your model objects... so if the table view showed recipe ingredients, it'd probably be the "Ingredient" class. You'll need to reference the class that defines these objects in the header file of both UIViewController subclasses (the one that shows the tableView and SubInfoViewController.h) - that is, if you haven't already.
Steve N
A: 

I found this very helpful and very similar to what 'Steve N' was saying.

Loading detail view

jimbo