views:

121

answers:

2

In my subclass of NSObject I would like to call something like

    [[self navController] presentModalViewController:myView animated:YES];

But none of my tries were successful. How can I call a modal view if I'm not in a subclass of UIViewController?

Solution:

#import "myProjectNameAppDelegate.h"
// ...
MyViewController *myView = [[MyViewController alloc] init];
myProjectNameAppDelegate *appDelegate = (myProjectNameAppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate navController] presentModalViewController:myView animated:YES];
A: 

I don't see a way to display a modal view without a ViewController. You have to store a reference to a UIViewController in your class so you can access it. Or setup a property in your AppDelegate, which you can get by calling [[UIApplication sharedApplication] delegate];

jv42
I have a property in my `AppDelegate` called "navController". Why can't I access ` [[[UIApplication sharedApplication] navController] presentModalViewController:myView animated:YES];` Do I have to declare another property and set this property to `self` in `didFinishLaunchingWithOptions:`?
testing
You should be able to access it directly as you pasted, does it work?
jv42
No, I get "'UIApplication' may not respond to '-navController'". With `delegate` I get "'-navController' not found in protocol(s)"
testing
You need to cast your app delegate to your real delegate.
jv42
How does this work? I know something like `AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];` but what is "AppDelegate"? Then I could do `[[appDelegate navController] presentModalViewController:myView animated:YES];`?
testing
I'm such a noob. OK the "AppDelegate" is the AppDelegate in my project. It is called something like "myProjectNameAppDelegate". I post the solution in my question.
testing
It's OK being a noob, as long as you learn ;-) Glad to be helpful there.
jv42
A: 

If you hold the navigationController or some viewController, you can present a modal view controller.

What is your myView? Is it a view, is it a viewController. I hope that it is a viewcontroller otherwise, this is the reason your code doesn't run

vodkhang
`myView` is a ViewController. `MyViewController *myView = [[MyViewController alloc] init];` But I can't call the modal view from the same ViewController (tried that, but nothing happens). I can't access the NavigationController because I'm in a 'NSObject' class. I get "xxx may not respond to xxx" warning.
testing
then, it makes sense that you cannot access navController. So, the good way is when you init your object, pass it a view Controller: `MyObject *obj = [[MyObject alloc] initWithViewController:myViewController];` then store your myViewController somewhere
vodkhang
When you need to presentModalViewController do: `[myViewController presentModalViewController animated:YES];`
vodkhang
I tried to find a solution for getting a ViewController reference. But I only thought about getting a property, which would include a new instantiation. Your solution would work. Thanks for that! I marked jv42 as solution, because I don't have to change two `NSObject` classes (add two init methods and change calls).
testing
ok, you are welcome. you can also vote up as well if you think that helps:)
vodkhang