3

I have created a UITabBarController in my app delegate. where each tab bar item holds a different UINavigationController that loads a custom UIViewController with a NIB (using -pushViewController:). Inside one of the navigation controller, I load a custom UIView class with a custom NIB also. This view is loaded multiple times inside the UIViewController.

The custom UIView has a UIButton, that on the event of touching it, I want to push a new UIViewController on the stack. Problem is that I 'lost' the UINavigationController that holds the UIViewController. I know I should use delegates, but haven't figured out who should which class should be the delegate.

Thanks in advance!

iHunter
  • 6,205
  • 3
  • 38
  • 56
omeraloni
  • 49
  • 1
  • 1
  • 4
  • delegate vould be better. the delegate should be defined in your view class and should be implemented by your owner. Secondly the owner property should be in UIView not in UIViewController. – Abid Hussain Dec 26 '12 at 12:35

2 Answers2

3

Neither .navigationController or .tabBarController will be available for a UIView or UIViewController that's been created but not pushed onto a stack

Either create a property on your View (or ViewController) class that is a UIViewController that is provided optionally after initialization or you could add a third argument to initWithNibName:bundle:

@interface CustomViewController : UIViewController
{ 
    UIViewController *owner;
}

@property (nonatomic, assign) UIViewController* owner;

@end

Then in the owner ViewController:

CustomViewController *cvc = [[CustomViewController alloc] initWithNibNamed:nil bundle:nil];
cvc.owner = self;

It's too bad .parentViewController is read-only, this would be the sensible place for this.

Cameron Hotchkies
  • 1,509
  • 1
  • 13
  • 20
  • Thanks for your reply. That's what I did, i've setup a property and after allocation and initialization I've set the owner as 'self'. But when use the getter [self owner] in e.g. viewDidLoad i get (null). If i do it in the owner ViewController: [cvc owner] i don't get (null). – omeraloni May 15 '11 at 13:50
  • Are you assigning the owner before adding it to the parent subview? – Cameron Hotchkies May 17 '11 at 19:04
  • Yes. I also release the view after adding it. – omeraloni May 21 '11 at 11:33
0

You can get this using UIApplication class. Using this class you can find which viewController is placed at first. Here is the solution link for your problem.

Community
  • 1
  • 1
arthankamal
  • 6,341
  • 4
  • 36
  • 51