21690

How to get parent navigation controller in UIView

Question:

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!

Answer1:

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.

Answer2:

You can get this using UIApplication class. Using this class you can find which viewController is placed at first. Here is the solution <a href="https://stackoverflow.com/questions/8585881/finding-the-rootviewcontroller-in-ios" rel="nofollow">link</a> for your problem.

Recommend

  • does GetComInterfaceForObject pin the object?
  • BlackBerry - Fun with FieldManagers
  • Dagger2 difference between modules and dependincies
  • ARC delegate memory management
  • Putting text shadow on a UIButton text label
  • this class is not key value coding-compliant for the key PerformRotate
  • Custom font for MKAnnotationView Callout
  • How to change default stop edit behavior in jtable
  • Get UILabel out of UIButton
  • Best practice for switching iPhone views?
  • Aligning image on right side of title on UIButton with imageEdgeInsets and titleEdgeInsets
  • Specify HTTPS for custom WCF Binding
  • Making Google Visualization - Annotation Chart to work in GWT
  • Debugging VB6 Code From Visual Studio 2010
  • How do I superscript characters in a UIButton?
  • How can I set a binding to a Combox in a UserControl?
  • d3 v4 drag and drop with TypeScript
  • JQuery Internet Explorer and ajaxstop
  • Atlas images wrong size on iPad iOS 9
  • Extracting HTML between tags
  • Ajax Loaded meta Tags
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Adding a button at the bottom of a table view
  • Android screen density dpi vs ppi
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Bug in WPF DataGrid
  • ActionScript 2 vs ActionScript 3 performance
  • Cannot Parse HTML Data Using Android / JSOUP
  • File upload with ng-file-upload throwing error
  • Free memory of cv::Mat loaded using FileStorage API
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • Linking SubReports Without LinkChild/LinkMaster
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?