views:

115

answers:

2

Hi,

I have my 'shake' working fine (using motionEnded), based off of Apple's GLPaint code. When the user shakes the device (running 3.0 and up) I want to open a view controller modally using presentModalViewController.

In my appdelegate I have the notification (as per the GLPaint sample code):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeToOpenHiddenScreen) name:@"shake" object:nil];

In my shakeToOpenHiddenScreen I just want to open view 'x' modally but I don't think that my appdelegate will respond to presentModalViewController.

Is there a way around this?

A: 

It is a method on UIViewController, so you should either have access to a saved view controller from your appDelegate, or else set up the notification to call one (addObserver:someVC).

"shake" isn't a standard notification name, so there should be some code elsewhere in your app that posts this notification, presumably also copied from the GLPaint sample.

Paul Lynch
+1  A: 

To use presentModalViewController you have to use it from a UIViewController class, or subclass:

For example: //RootViewController.m [self.navigationController presentModalViewController:loginRegView animated:YES];

You can way around this problem by defining a navigation controller into your app delegate:

//yourApp_comAppDelegate.h
UINavigationController *nav;
...
@property(nonatomic,retain) UINavigationController *nav;

and synthesize it

@syntetize nav;    

To use presentModalViewController you have to use it from a UIViewController class, or subclass:

For example:

//RootViewController.m
[self.navigationController presentModalViewController:loginRegView animated:YES];

You can way around this problem by defining a navigation controller into your app delegate:

//yourApp_comAppDelegate.h
UINavigationController *nav;
...
@property(nonatomic,retain) UINavigationController *nav;

synthesize it

//yourApp_comAppDelegate.m
@synthesize nav;

and now you can use the method:

//yourApp_comAppDelegate.m
[nav presentModalViewController:yourView animated:YES];

but, first you have to assign it somewhere, i will do it in the RootViewController

//RootViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
app = (yourApp_comAppDelegate *) [[UIApplication sharedApplication] delegate];
    app.nav = self.navigationController
}

It should work, let me know :)

Cesar
You should edit this to remove the repeated text. Also, a nave controller is no more useful for this purpose than any other view controller, and there should ideally be a view controller called from the app delegate in any app.
Paul Lynch
thx for the advices
Cesar