Add property at runtime + click on blank to hide UIAlertView

Question: Not long after I took over a project, I submitted a bug in the test today, which is a logical bug. The front desk received a message sent by mqtt, and then displayed a pop-up box with UIAlertView with two buttons of " Join " and "Reject" to choose from. That is to say, when the user performs an operation on a certain page, a prompt box suddenly pops up, and I have to choose one. But if the user doesn't want to decide now, he needs to hide the pop-up window. There is no cancel button at this time, so there is a requirement: click on the black background of UIAlertView to hide the pop-up window.

The previous code is like this:

UIAlertView *organizationAlert = [[UIAlertView alloc]initWithTitle:nil

                                                    message:[NSString stringWithFormat:@"Dear user, your platform account was invited to join by %@, please confirm.",notifica.object]

                                                  delegate:self

                                          cancelButtonTitle:@"拒接"

                                          otherButtonTitles:@"加入", nil];

    [kUserDefaults setObject:[NSString stringWithFormat:@"Dear user, your platform account was invited to join by %@, please confirm.",notifica.object] forKey:@"NotificaObject"];

    [self.organizationAlert show];

Now I want to add a click event and then dismiss the popup in the event. At this time, organizationAlert needs to be written as a property. Here comes the problem: it's all written in the category!

#import "AppDelegate.h"

@interface AppDelegate (Notifies)

@end

Got it, to add attributes here, you need to use runtime, which is also one of the few application scenarios that runtime needs to use: adding attributes.

Put the complete code directly below:

① Write the pop-up window as an attribute

#import "AppDelegate.h"@interface AppDelegate (Notifies)

@property (nonatomic, strong) UIAlertView *organizationAlert;

@end

②Add getter setter method

//strKey unique identifier

static void *strKey = &strKey;

- (void)setOrganizationAlert:(UIAlertView *)organizationAlert{

    objc_setAssociatedObject(self, &strKey, organizationAlert, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

- (UIAlertView *) organizationAlert {

    return objc_getAssociatedObject(self, &strKey);

}

③Use

#pragma mark -- organization invitation

-(void)organizationInvite:(NSNotification *)notifica{

    self.organizationAlert = [[UIAlertView alloc]initWithTitle:nil

                                                    message:[NSString stringWithFormat:@"Dear user, your platform account was invited to join by %@, please confirm.",notifica.object]

                                                  delegate:self

                                          cancelButtonTitle:@"拒接"

                                          otherButtonTitles:@"加入", nil];

    [kUserDefaults setObject:[NSString stringWithFormat:@"Dear user, your platform account was invited to join by %@, please confirm.",notifica.object] forKey:@"NotificaObject"];



    [self.organizationAlert show];



    //click background to hide

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];

    tap.numberOfTapsRequired = 1;

    tap.cancelsTouchesInView = NO;

    [[UIApplication sharedApplication].keyWindow addGestureRecognizer:tap];

}

//click event handler

- (void)tap:(UITapGestureRecognizer *)tap

{

    if (tap.state == UIGestureRecognizerStateEnded){//The following code to understand by yourself

        CGPoint location = [tap locationInView:nil];

        if (![self.organizationAlert pointInside:[self.organizationAlert convertPoint:location fromView:self.organizationAlert.window] withEvent:nil]){

            [self.organizationAlert.window removeGestureRecognizer:tap];

            [self.organizationAlert dismissWithClickedButtonIndex:0 animated:YES];

        }

    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324521685&siteId=291194637