I tried Alex's suggestion buy making a category for UIViewController, and it worked in the simulator, but not on my phone. here is the category
@interface UIViewController (parentSetter)
-(void)setParentUIViewController:(UIViewController*)parent;
@end
@implementation UIViewController (parentSetter)
-(void)setParentUIViewController:(UIViewController*)parent
{
_parentViewController = parent;
}
@end
It compiles and works fine, but note the underscore member which is a bit off putting.
That's what causes the linker error when compiling against the 3.0 SDK.
I have a container view that has 2 subviews and a table is one of them. The table needs a parent so it can interact with the navigation bar, among other things.
I'm going with this solution instead:
@interface AdoptedTableViewController : UITableViewController {
UIViewController* surrogateParent;
}
-(UINavigationController*)navigationController;
@property (nonatomic, assign) IBOutlet UIViewController *surrogateParent;
@end
@implementation AdoptedTableViewController
@synthesize surrogateParent;
-(UINavigationController*)navigationController
{
if( [super navigationController] )//self.navigationController )
{
return [super navigationController];
}
else
{
return surrogateParent.navigationController;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
All of my table view controllers are now adoptedTableViewControllers. The main reason they need parents is so they can push view controllers on to the navigation stack, so that is handled transparently by the navigation controller getter.
It would be nice if parentViewController were not read only, but in my dabble with _parentViewController I discovered there is more to the ViewController hierarchy than just that property. I think there might be a lot of coupling and responsibilities in that relationship that Apple hasn't cleaned up enough for the masses. For instance, I noticed an odd deselection behavior when moving up the navigation hierarchy that I couldn't fix. Perhaps UINavigation controllers reflect the class of their top controller and behave differently?
In short, it really is read only and there is no clean or simple workaround. You just got to architect around it.