I am having some difficulty trying to figure out how to pass an item between the Views in my applications. My program is a NavigationController based app that starts out with a table containing information about each item. When the user clicks on an item I want to take them to an ItemViewController nib that takes an Item object and populates the fields on the page based on the properties of the Item. The trouble is, no matter how many different ways I try to set the Item property of ItemViewController, it always end up null when the view loads. The item object is obtained programatically in the table view's click event on each row and then I initialize the ItemViewController and call its setItem method to specify the object. I attempted to implement my own setter method and when I stepped into it a call to self.item returned the correct object, but as soon I try to retrieve the item with my getter it is null.
Where did my object go?
Additionally, could someone show me how to manually implement my properties so that they are not generated for me? I'd like to slap some breakpoints in the setters to see if I can get a better feel for what's going on.
RootViewController.m (excerpt)
ItemViewController.h
Item.h
Where did my object go?
Additionally, could someone show me how to manually implement my properties so that they are not generated for me? I'd like to slap some breakpoints in the setters to see if I can get a better feel for what's going on.
RootViewController.m (excerpt)
Code:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
iPhoneHelloWorldAppDelegate *appDelegate = (iPhoneHelloWorldAppDelegate *)[[UIApplication sharedApplication] delegate];
Item *item = (Item *)[appDelegate.items objectAtIndex:indexPath.row];
if(self.itemView == nil) {
ItemViewController *viewController = [[ItemViewController alloc] initWithNibName:@"ItemViewController" bundle:[NSBundle mainBundle]];
self.itemView = viewController;
[viewController release];
}
[self.itemView setItem: item]; // self.itemView.item is always nil. even when item is not
[self.navigationController pushViewController:self.itemView animated:YES];
self.itemView.title = [item itemName];
//[item release];
//[self.itemView.itemDescription setText:[item itemDescription]]; // This call works fine.
]
ItemViewController.h
Code:
@interface ItemViewController : UIViewController {
IBOutlet UITextView *itemDescription;
Item *item;
SellViewController *sellView;
NSString *unique;
}
@property (nonatomic, retain) IBOutlet UITextView *itemDescription;
@property (nonatomic, retain) IBOutlet Item *item;
@property (nonatomic, retain) SellViewController *sellView;
@property (readwrite, assign) NSString *unique;
-(IBAction) sellItem : (id)sender;
@end
Item.h
Code:
@interface Item : NSObject {
NSString *itemName;
NSString *itemDescription;
int *itemPrice;
NSString *unique;
}
@property (nonatomic, retain) NSString *itemName;
@property (nonatomic, copy) NSString *itemDescription;
@property (readwrite, assign) int *itemPrice;
@property (nonatomic, retain) NSString *unique;
-(id)initWithName:(NSString*)name description:(NSString*)desc price:(int*)price;