In my app I am using a navigation controller that uses a table view to organize data. Now when the user selects a row a detail view pushed onto the stack. This detail view is the same no matter which row is selected. The only difference is that depending on which row was selected, my detail view will load data corresponding to the selected row from a saved .plist file.
So I 开发者_C百科was wondering how can I inform my detail view about which row was selected in my table view.
Can you not simply just create a custom init method in the view controller? For example, I pass a selected date to a ViewController using the following:
.. interface
- (id)initWithNibNameandDate:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dateValue:(NSDate *) selectedDate;
... implementation
- (id)initWithNibNameandDate:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dateValue:(NSDate *) selectedDate {
self.selDate = selectedDate;
return [super initWithNibName:nibNameOrNil bundle:nil];
}
It really doesn't matter what data type you are passing over.
Hope this helps.
Within your table view controller, the didSelectRowAtIndexPath: method will be called when the user selects a row. The returned value indexPath provides the section and row numbers of the selected row. From that, you can work out the item in question (e.g. its the nth in the array you've got them from) and that can be passed to the detail view controller.
Actually it was as simple as adding an NSUInteger ivar in the detail view controller and then simply setting its value from the tables delegate method just before I push the view onto the stack. Thanks for your answers guys.
精彩评论