Objective-C – Getting a UIViewController reference

2019-08-08 16:41发布

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条回答
狗以群分
2楼-- · 2019-08-08 17:20

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];
查看更多
登录 后发表回答