问题
I'm way in deep water here trying to get ahold of a vc.
My App layout looks like this:
TabBarController (holds a) -> UINavController for Tab 1 (holds a) -> MyOwnListViewController -> UINavController (modally) (holds a) -> ItemAddViewController -> AddItemToItemViewController
Maybe that wasn't very clear, so for clarity, when the app starts I'm on tab 1 and the MyOwnListViewController
is visible, then I push a + button in the navigation bar and I'm taken modally to the ItemAddViewController
. From there I pushViewController AddItemToItemViewController
. Now in this view controller I want to get a reference to ItemAddViewController
.
What would be the easiest way to get a reference to it?
回答1:
You could modify the init*** method of your AddItemToItemViewController and pass it the reference on your ItemAddViewController.
E.G :
In AddItemToItemViewController.h :
@property (nonatomic, retain) UIViewController *parentController;
In AddItemToItemViewController.m :
@synthesize parentController;
-(id) initWithParentController:(UIViewController *) controller{
self = [super init];
if(self){
self.parentController = controller;
}
return self;
}
-(void) dealloc{
[self.parentController release];
[super dealloc];
}
When calling your controller inside your ItemAddViewController instance :
UIViewController *controller = [[AddItemToItemViewController alloc] initWithParentController:self];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
来源:https://stackoverflow.com/questions/9099961/objective-c-getting-a-uiviewcontroller-reference