如何在iOS中为推送通知警报视图设置操作

时间:2012-06-12 09:03:32

标签: iphone action push-notification uialertview

在我的应用程序中,我实现了推送通知功能,我收到了通知。我在appDelegate文件中使用了以下代码。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    for (id key in userInfo) {
        NSMutableArray *array = [userInfo objectForKey:key];
        NSString *message = [NSString stringWithFormat:@"%@",[array valueForKey:@"alert"]];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"iPhoneApp" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
        [alert show];
        [alert release];
    }    
}

我想对推送通知提醒的OK按钮点击事件(当应用程序运行时)执行操作。我在这个应用程序中有三个视图控制器。那么我应该在哪个类中添加代码

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

1 个答案:

答案 0 :(得分:0)

在同一个类中,您设置为该特定UIAlertView的delegate。在您目前的情况下,AppDelegate-clickedButtonAtIndex:的接收者。

如果您希望收到3个控制器之一的点击事件。您必须将该特定控制器设置为UIAlertView

的代理人
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"iPhoneApp" message:message delegate:myViewController cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    [alert show];
    [alert release];

如您所见,我已将myViewController指定为代表。 myViewController应符合UIAlertViewDelegate协议并实施-clickedButtonAtIndex:方法。现在,当您选择其中一个按钮后,您将在myViewController中接听电话。

相关问题