Touch events in appDelegate

2019-08-05 11:49发布

问题:

Here's my problem.

I have implemented push notification in my app and it works just fine.

  • The app receives a message
  • Captured in appDelegates: didReceiveRemoteNotification
  • Creates a new UIView to display that a new message is received

Only problem I have is that I want the user to be able to tap on the message so that the app will open the message viewcontroller. But I can't seem to figure out how to capture this touch and then do something with it in the appDelegate. I have an NSLog in the method that should open the new viewcontroller but it seems that it is never called.

Any help would be appreciated!

Here's the code, it is in the didReceiveRemoteNotification method:

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoMessages)];
    UIView *messageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
    messageView.userInteractionEnabled = YES;
    messageView.backgroundColor = [UIColor darkGrayColor];
    [messageView addGestureRecognizer:recognizer];
    [self.window addSubview:messageView];

Thanks!

回答1:

I recommend that you not use your app delegate class for this. That's not what it's for and it will lead to disorganized design.

Why aren't you using a regular view controller and a UIView subclass? They are the natural places for gesture and touch input.

As for why your NSLog is not appearing, it might be helpful to show that code as well.

Your tap gesture recognizer appears configured to allow any tap anywhere on the screen, as opposed to tapping right on the message, as you mention. Just FYI.