Reputation: 1131
I have a rootcontroller pushed on a UINavigationController
.
Inside that rootcontroller class, I can access the UINavigationController
with this.NavigationController
However, this rootcontroller has a ScrollView and I'm adding subcontrollers (or more precise, the View of this subcontroller) to this ScrollView.
I would now like to access the UINavigationController from inside such subcontroller. Following properties are all null
this.NavigationController
this.ParentViewController
this.PresentedViewController
this.PresentingViewController
It seems in ObjectiveC you can use following code
YourAppDelegate *del = (YourAppDelegate *)[UIApplication sharedApplication].delegate;
[del.navigationController pushViewController:nextViewController animated:YES];
Unfortunately, i don't know how to map this to C# in MonoTouch. I tried the following, but it's not working:
UIApplication.SharedApplication.KeyWindow.RootViewController.NavigationController
I know I could pass the UINavigationController object to all my classes (parameter in constructor), but that's probably not the cleanest way to go.
Upvotes: 3
Views: 900
Reputation: 8170
To extend poupou's answer, here is an example of what I usually do in my AppDelegate
class:
Add a static property of the AppDelegate
itself:
public static AppDelegate Self { get; private set; }
Add my root navigation controller as a property:
public UINavigationController MainNavController { get; private set; }
In FinishedLaunching
:
Self = this;
window = new UIWindow(UIScreen.MainScreen.Bounds);
this.MainNavController = new UINavigationController(); // pass the nav controller's root controller in the constructor
window.RootViewController = this.MainNavController;
// ..
This way, I can access the root view controller from anywhere, like this:
AppDelegate.Self.MainNavController.PushViewController(someViewController);
... instead of having to write this all the time:
AppDelegate myAppDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
myAppDelegate.MainNavController.PushViewController(someViewController);
Plus, I can directly access all other AppDelegate
's properties I might have.
Hope this helps.
Upvotes: 4
Reputation: 43553
UIApplicationDelegate
does not, itself, define a navigationController
property.
OTOH [UIApplication sharedApplication].delegate
returns the instance of your own, application specific, UIApplicationDelegate
- so it's a great place to share stuff (and why it's frequently used).
What commonly happens, in ObjectiveC, is that this custom ,UIApplicationDelegate
-derived, type will implement it's own application-wise properties. IOW YourAppDelegate
will implement a navigationController
property that can be accessed, anywhere inside the app, by using [UIApplication sharedApplication].delegate
.
You can do something very similar in .NET / C#. Simply add your own properties to your AppDelegate type, like this example. You'll be able to access them like Objective-C (if you like) or more directly (e.g. by making them public static
properties).
Note that you still have to track and set your properties correctly (just like it needs to be done in Objective-C too).
Upvotes: 2