UIAlertView 委托方法未调用?

UIAlertView delegate meethod is not calling?

当我收到推送通知时,我在 appDelegate 中调用此方法

-(void)activeNotification:(NSDictionary *)userInfo{
    NSLog(@"%@",userInfo);
    NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];


    ViewController *view=[[ViewController alloc]init];
    [view checkNotification:message];
}

我从这里调用 ViewController.m

中定义的方法 checkNotification:message()
-(void)checkNotification:(NSString *)message{
        NSLog(@"%@",message);
        NSArray *arr=[[NSArray alloc]init];

    if([message hasPrefix:@"taxi"])
    {
        arr=[message componentsSeparatedByString:@":"];

        alertV=[[UIAlertView alloc]initWithTitle:@"Taxi" message:[arr objectAtIndex:1] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertV show];

    }
 }

当我点击确定按钮时,我需要调用 alertview 委托方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
}

但是这个方法没有被调用请帮助我

在我的ViewController.h

@interface ViewController : UIViewController<FBLoginViewDelegate,UIAlertViewDelegate>

我不知道为什么没有调用 AlertView 委托..

问题是,您提供了错误的委托。您的委托是临时 ViewController 在 AppDelegate 中创建的。它会在方法 activeNotification:(NSDictionary *) returns 后立即销毁。但是您稍后会单击该按钮。您必须确保代表的生命周期。

您也可以使用另一个委托,例如直接使用 AppDelegate 或 RootViewController。

正如 Paulw11 指出的那样,下面的方法不起作用,因为从方法 instantiateViewControllerWithIdentifier:(NSString *):

返回了一个新实例
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                     bundle: nil];
MyViewController *controller = (MyViewController*)[mainStoryboard 
                instantiateViewControllerWithIdentifier: @"<Controller ID>"];

您有多个问题。正如@Robse 所说,您正在您的应用程序委托中创建一个新的视图控制器实例,它没有连接到任何东西并试图在该实例中调用一个方法。这没有任何意义,而且在不进一步了解您的应用程序的情况下很难告诉您如何修复它。

您需要一种方法来获取当前的前视图控制器并在该视图控制器上显示警报。为了帮助您解决这个问题,您将不得不解释如何在您的应用程序中创建和显示视图控制器。你在使用故事板吗?当您收到推送通知时,您能确定显示的是哪个视图控制器吗?

好的,根据您的评论,我猜您的视图控制器正在由您应用的主故事板加载,并且是您应用的初始视图控制器。这是在启动时自动加载的视图控制器。

要访问应用主 window 的根视图控制器,请使用如下代码:

[UIApplication sharedApplication].keyWindow.rootViewController

因此,您的代码将如下所示:

-(void)activeNotification:(NSDictionary *)userInfo{
NSLog(@"%@",userInfo);
  NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];

  ViewController *rootVC;
  rootVC = (ViewController *) [UIApplication sharedApplication].keyWindow.rootViewController;
  [rootVC checkNotification:message];
}

该代码向应用程序询问应用程序的键 window,然后向应用程序询问根视图控制器的键 window,在您的情况下,它应该是应用程序的起始视图控制器

编辑:没关系。我从您较新的评论中看到,您的默认视图控制器不是您的视图控制器,而是您有一个导航控制器。在这种情况下,您应该改用 Paulw11 的代码。

当您post提问时,请尽量提供对您的问题的完整描述。

以下对您的问题的描述会好得多:

I have an app that uses storyboards." The initial view controller is a navigation controller, and then the root view controller of the navigation controller is an instance of my ViewController class. I'm trying to send a message to my ViewController when the app delegate receives a push notification.

这样我们就可以一次性回答您的问题了。

请注意,考虑如何清楚、完整地描述您的问题,以便其他人帮助解决问题,通常会触发您自己寻找问题。

您的代码正在分配 ViewController 的新实例,但您需要获取对视图层次结构中实际视图控制器的引用。

您说您的 ViewController 在 UINavigationController 中,因此假设它是您显示的第一个视图控制器,它将位于导航堆栈的根部。您可以使用以下代码获取对它的引用 -

-(void)activeNotification:(NSDictionary *)userInfo{
    NSLog(@"%@",userInfo);
    NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];

    UINavigationController *navC=(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;

    ViewController *view=navC.viewControllers[0];
    [view checkNotification:message];
}