EDIT 2: I tried pointing to the view controller using the following code:
UIStoryboard *iPadStoryboard = [UIStoryboard storyboardWithName:@"iPadStoryboard" bundle: nil]; GTGiftsIPadViewController *giftsIVC = (GTGiftsIPadViewController *)[iPadStoryboard instantiateViewControllerWithIdentifier: @"giftsTableViewControllerId"]; [giftsIVC setGiftsDelegate:self]; NSLog(@"%@", giftsIVC); NSLog(@"%@", [[[[[[self parentViewController] parentViewController] childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]);
However, this outputs:
2012-06-02 14:28:47.148 Gift Manager[3958:707] <GTGiftsIPadViewController: 0xc6b9980>
2012-06-02 14:28:47.152 Gift Manager[3958:707] <GTGiftsIPadViewController: 0xc6a16d0>
Meaning that giftsIVC is not pointing to the proper view controller. It has to point to the view controller given by [[[[[[self parentViewController] parentViewController] childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]. However, I cannot use this code because it is does not work for both the portrait and landscape view. This seems like something that should be very straight forward to do, but I'm pulling my hair out trying to figure this out! It's driving me crazy!
EDIT: I figured out the problem and it has to do with how I'm setting the delegate. This is my (poor) way of setting the delegate:
[[[[[[[self parentViewController] parentViewController] childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0] setGiftsDelegate:self];
Basically [[[[[[[self parentViewController] parentViewController] childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0] is just referencing my table view controller. Is there an alternative way I could write this bit of code to exclude the [[[[[[[self parentViewController] parentViewController] childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0] portion?
I have been trying to avoid creating my own delegate protocols, mainly because I'm not very comfortable with the idea of a delegate yet. Anyway, I went ahead and created one yesterday because I really needed it, and to it worked very well! I have a split view controller that displays two controllers when the iPad is in landscape mode (one table view and a detail view for the objects in the table view). The purpose of the delegate protocol is to allow the table view to update the detail view when an object in the table view is selected. This works in landscape mode; however, in portrait mode I only have one view controller and a button to display a popover that presents the table view (this is almost identical to the Notes app on the iPad).
My problem is that when in portrait mode and a object in the popover view table is selected the didSelectGift method is not called; however, in landscape mode it is. See capitalized comments below.
This is my code for the delegate protocol:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "GTAddIPadViewController.h"
@class GTGift, GTGiftCell;
@protocol GTGiftsIPadViewControllerDelegate;
@interface GTGiftsIPadViewController : UITableViewController
@property (nonatomic, assign) id <GTGiftsIPadViewControllerDelegate>giftsDelegate;
@property (nonatomic, strong) IBOutlet UITableView *giftsTable;
@end
@protocol GTGiftsIPadViewControllerDelegate <NSObject>
- (void)didSelectGift:(GTGift *)selectedGift;
@end
didSelectGift is called in the GTGiftsIPadViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
GTGift *gift = [[[GTGiftStore sharedStore] allGifts] objectAtIndex:[indexPath row]];
[giftsDelegate didSelectGift:gift]; //THIS FUNCTION IS NOT CALLED (TESTED BY PUTTING AN NSLOG INSIDE IT)
NSLog(@"%@", [gift name]); //THIS OUTPUTS PROPERLY THEREFORE TABLEVIEW:DIDSELECTROWATINDEXPATH: IS CALLED
}
The delegate is set the viewDidLoad method in a view controller called GTDetailIPadViewController
The didSelectGift method is implemented in the GTDetailIPadViewController:
- (void)didSelectGift:(GTGift *)selectedGift
{
[self setDetailGift:selectedGift];
[self populateTable];
}
(populateTable is a method that uses the instance variable of detailGift and populates the detail view with these values)
I really cannot understand why it works perfectly when the table is presented in the split view but does not work when it is presented in the popover view. Any help would be really appreciated and I hope I have made my problem clear! Thank you!