views:

1084

answers:

2

I've created a UIViewController (AnestDrugCalcViewController) that has a button that opens a UITableViewController (DrugTableViewController) to select a drug. The DrugTableViewController is populated based on a NSMutableArray and is made up of several NSDictionary objects (drugName, drugDose, drugConcentration, etc.).

I'm able to open DrugTableViewController by pushing it onto the navigation stack, but I can't figure out how to tell from my AnestDrugCalcViewController what the properties of the selected item are.

//  DrugTableViewController.h
#import <UIKit/UIKit.h>

@interface DrugTableViewController : UITableViewController {
NSMutableArray   *drugList;
NSIndexPath *lastIndexPath;
IBOutlet UITableView *myTableView;
NSObject *selectedDrug;
}

@property (nonatomic, retain) NSArray *drugList;
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) NSObject *selectedDrug;

@end

Top of DrugTableViewController.m #import "DrugTableViewController.h"

@implementation DrugTableViewController

@synthesize drugList,myTableView, selectedDrug;
+3  A: 

You could have a property in vwDrugTable that stores the the selected row when the user selects it. Then, after that table view is removed and you are back in vwCalc, just get the data from that property.

So that means in vwDrugTable, have something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedDrug = [self.drugs objectAtIndex:indexPath.row];
}

Then just access selectedDrug from vwCalc when you need it.

Marc W
Sorry -- I'm a noob -- The two views have separate view controllers. I've created selectedDrug as an NSObject and can access it from vwDrugTable, but how do I access it from vwCalc? I've included DrugTableViewController.h in CalculatorViewController.m and have defined the drugListViewController as a local variable (also including @property in .h and @synthesize in .m).When I try to access the property (NSLog(@"%@"),drugListViewController.selectedDrug), it refuses to compile ("request for member 'selectedDrug' in somthing not a structure or union)What am I missing? I a
Chris Brandt
Edit your question and show me your .h and top of your .m (where @synthesize sits) for DrugTableViewController. That will help me help you.
Marc W
+3  A: 

UITableView's -indexPathForSelectedRow method will allow you to determine which row is selected:

NSIndexPath *selectionPath = [vwDrugTable indexPathForSelectedRow];
if (index) {
    NSLog(@"Selected row:%u in section:%u", [selectionPath row], [selectionPath section]);
} else {
    NSLog(@"No selection");
}

To be notified when the selection changes, implement tableView:didSelectRowAtIndexPath: in your table view delegate:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Selection Changed");
}
rpetrich
Also, hungarian notation is not recommended in Objective-C.
rpetrich