从UILocalNotification呈现View Controller的问题

时间:2022-09-06 21:17:02

I'm working with local notifications in iOS for the first time, and I was wondering how to push to a specific view from the notification that is not the default ViewController on launch. From looking at other questions I have the code

我是第一次使用iOS中的本地通知,我想知道如何从启动时的默认ViewController通知中推送到特定视图。从查看其他问题我得到了代码

-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
   [self.window.rootViewController presentViewController:gameViewController animated:NO completion:nil];
}

gameViewController is the name of the file of the ViewController I want to push (it is not in a Navigation controller). The error I receive is "Use of undeclared identifier 'gameViewController'. I realize this is because it is not declared in the appDelegate, but how do I do so? When I declared it as a property the code compiled, but crashed when I pressed on the notification. Thanks for the help!

gameViewController是我要推送的ViewController文件的名称(它不在导航控制器中)。我收到的错误是“使用未声明的标识符'gameViewController'。我意识到这是因为它没有在appDelegate中声明,但是我该怎么做?当我将它声明为属性代码编译时,但是当我按下时崩溃了在通知上。感谢您的帮助!

1 个解决方案

#1


0  

You will need to declare the class and a property in appdelegate.h see code below:

您需要在appdelegate.h中声明类和属性,请参阅下面的代码:

#import "gameViewController.h"
@class gameViewController;

 @interface XAppDelegate : UIResponder <UIApplicationDelegate >

@property (nonatomic, strong) gameViewController *gameVC;

then in your app delegate.m - synthesise the property

然后在你的app delegate.m中 - 合成属性

  @synthesize gameVC;
    .....

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

 {       
...  
 gameVC= [[gameViewController alloc] initWithNibName:@"gameViewController" bundle:nil];
 ....
}


  -(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

 [self.window.rootViewController presentViewController:gameVC animated:NO completion:nil];

app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber-1;



 } 

Most people including myself use UIAlertviews instead of modal views for local notification but the code above should work as well (saves the effort of creating a separate VC).

包括我在内的大多数人都使用UIAlertviews而不是模态视图来进行本地通知,但上面的代码也可以正常工作(节省了创建单独VC的工作量)。

#1


0  

You will need to declare the class and a property in appdelegate.h see code below:

您需要在appdelegate.h中声明类和属性,请参阅下面的代码:

#import "gameViewController.h"
@class gameViewController;

 @interface XAppDelegate : UIResponder <UIApplicationDelegate >

@property (nonatomic, strong) gameViewController *gameVC;

then in your app delegate.m - synthesise the property

然后在你的app delegate.m中 - 合成属性

  @synthesize gameVC;
    .....

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

 {       
...  
 gameVC= [[gameViewController alloc] initWithNibName:@"gameViewController" bundle:nil];
 ....
}


  -(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

 [self.window.rootViewController presentViewController:gameVC animated:NO completion:nil];

app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber-1;



 } 

Most people including myself use UIAlertviews instead of modal views for local notification but the code above should work as well (saves the effort of creating a separate VC).

包括我在内的大多数人都使用UIAlertviews而不是模态视图来进行本地通知,但上面的代码也可以正常工作(节省了创建单独VC的工作量)。