点击通知按钮后执行转换

时间:2012-08-05 10:38:02

标签: ios xcode push-notification appdelegate

如果应用处于活动状态,我必须关注代码以显示通知。我把它放在AppDelegate.m

我想要做的是当用户点击第二个按钮时,对viewcontroller执行转换(或segue)。我怎么能从AppDelegate做到这一点?

我想我需要将navigationcontroller设置为appdelegate ..但我无法实现这一点。

由于

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        NSString *cancelTitle = @"Close";
        NSString *showTitle = @"Show";
        //NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Social Dilemma"
                                                            message:@"Next round is ready to play!"
                                                           delegate:self
                                                  cancelButtonTitle:cancelTitle
                                                  otherButtonTitles:showTitle, nil];
        [alertView show];
    }
}

-(void) alertView: ( UIAlertView *) alertView
clickedButtonAtIndex: ( NSInteger ) buttonIndex {
    if (alertView.tag == 1)
    {
        //check the button index
        //create and display the other alert view (set the tag property here to 2)
    }
    else if (alertView.tag == 2)
    {

    }
}

1 个答案:

答案 0 :(得分:0)

作为一个选项你可以使用uinavigationcontroller,这取决于你的应用程序有多少视图控制器。以简单的方式,您可以尝试调用当前的模态视图控制器到应用程序的根控制器:  1)添加到项目自定义UIViewController(.h,.m,.xib)(示例名称:MyViewController)

2)

-(void) alertView: ( UIAlertView *) alertView
    clickedButtonAtIndex: ( NSInteger ) buttonIndex {
    if (alertView.tag == 1)
    {
       //check the button index
       //create and display the other alert view (set the tag property here to 2)
       MyViewController *first = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
       [self.window.rootViewController presentModalViewController:first animated:YES];
        [first release];
    }
    else if (alertView.tag == 2)
   {
         MyViewController *second = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
       [self.window.rootViewController presentModalViewController:second animated:YES];
        [second release];
   }
 }

对于使用uinavigation控制器,在应用程序didFinishLaunchingWithOptions中:您可以以编程方式创建导航控制器,将其分配给根控制器并在警报视图的方法调用中

[self.window.rootViewController pushViewController:second (first) animated:YES];