Touch events in appDelegate

旧时模样 提交于 2019-12-11 11:11:45

问题


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.



来源:https://stackoverflow.com/questions/16057210/touch-events-in-appdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!