0

how to get a reference to some view controller in my app?, I do not need a new copy of the view controller I just need a reference to that controller so I can use its methods and update its properties.

thanks

flashdisk
  • 3,652
  • 3
  • 16
  • 22
  • You can pass a reference to it to any View Controllers that need it, but ask yourself why you need to do this, maybe your design could be improved better? – Tim Feb 24 '14 at 17:12
  • how to get a ref for that view controller?! – flashdisk Feb 24 '14 at 17:13
  • 2
    You need to PASS it when you create the new View Controllers. E.g. [[YourViewController alloc] initWithOtherViewController:self]; Then store it, and reference it as you need it. It would help if you showed us some code, and what you have already? – Tim Feb 24 '14 at 17:14
  • I am in some view controller and I need a ref to another view controller which has a variables that must be updated from my current view controller any thing with ALLOC is wrong! – flashdisk Feb 24 '14 at 17:16
  • What references do you already have? Can you trace it back to your App Delegate? – Daddy Feb 24 '14 at 17:18
  • I do not have any,I do not know how to create one! – flashdisk Feb 24 '14 at 17:22
  • So you want to update a progress bar? Sounds like a good opportunity to create a protocol for a Delegate. – Hyperbole Feb 24 '14 at 17:28
  • yes, because of that I need a ref to that view controller! – flashdisk Feb 24 '14 at 17:49

4 Answers4

1

Is there a parent-child relationship between the two?

If so, you might do something like

@interface ChildVC : UIViewController

@property (nonatomic, assign) ParentVC *parent;

@end

and in the ParentVC:

- (void)methodThatShowsOrCreatesChildVC
{
    // ...
    ChildVC *childVC = [[ChildVC alloc] init];
    childVC.parent = self;
    // ...
}

In the ChildVC:

- (void)methodThatChangesSomethingOnParent
{
    [[self parent] changeSomethingOnParent:something];
}

If there is not a parent-child relationship, this sounds like unnecessary coupling. Rather than that, you could try:

  • post a NSNotification about changes made
  • have a shared NSObject-based class that contains the shared "concerns".
Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
  • yes I want to get a ref for the parent view controller to update a progress bar in it from the child root controller! – flashdisk Feb 24 '14 at 17:22
0

It depends on your implementation, if the viewController is visible or is in the memory, you can access it in an other class, by creating a property and then accessing its attributes (properties, methods).

zaheer
  • 893
  • 13
  • 27
  • I am using push and pop that means it is in the memory! – flashdisk Feb 24 '14 at 17:17
  • can you show an example of how to create a property?! – flashdisk Feb 24 '14 at 17:18
  • If you are using storyboard and push/pop stack, access that as below: UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Storyboard~IPhone" bundle:[NSBundle mainBundle]]; UINavigationController * myStoryboardInitialViewController = [storyboard instantiateInitialViewController]; [myStoryboardInitialViewController.viewControllers objectAtIndex:0]; else access the controller using [self.navigationController.viewContrllers objectAtIndex:0]; – zaheer Feb 24 '14 at 17:24
  • your solution is creating new view controller and is not giving me a ref to the desired view controller – flashdisk Feb 24 '14 at 17:59
0

When you push a new view controller with

performSegueWithIdentifier:

you'll get the delegate method

prepareForSegue:

Which passes a UIStoryBoardSegue that has properties destinationViewController and sourceViewController

and from this answer:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segueName"])
    {
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}
Community
  • 1
  • 1
JuJoDi
  • 14,627
  • 23
  • 80
  • 126
0

If you are using a UINavigationController, you can do this to get the View Controller at any index

UIViewController *targetViewController = [self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count]-1)];
unspokenblabber
  • 1,567
  • 1
  • 11
  • 19