jcad
jcad

Reputation: 63

UIAlertController Action isnt working?

I have added a UIAlterController to my view. IT triggers fine, looks fine, and has its action, however tapping the Cancel action does nothing, it doesnt run any code, even if i add a block it wont execute it, it just doesnt seem to recognise the tap at all.

The code looks fine to me, so is there some other cause?

- (void)connectionFailed:(NSString *)title {
NSString *message = nil;
if ([title isEqualToString:NSLocalizedString(@"Connection Refused", @"connection Refused")]) {
    message = NSLocalizedString(@"You do not have permission to use this console", @"incorrect permissions message");
}
else {
    message = NSLocalizedString(@"Connection Failed", @"connection failed error message");
}

dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertController *alertController = [UIAlertController
                                alertControllerWithTitle:title
                                message:message
                                preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                  style:UIAlertActionStyleDefault
                                  handler:nil];

    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
});

Edit: it appears the issue is down to view leaks causing problems, not sure of the fix but here is the breakdown in debugging:

debugging image of view stack

Upvotes: 1

Views: 355

Answers (4)

Zღk
Zღk

Reputation: 884

- (void)connectionFailed:(NSString *)title {
NSString *message = nil;
if ([title isEqualToString:NSLocalizedString(@"Connection Refused", @"connection Refused")]) {
    message = NSLocalizedString(@"You do not have permission to use this console", @"incorrect permissions message");
}
else {
    message = NSLocalizedString(@"Connection Failed", @"connection failed error message");
}

dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertController *alertController = [UIAlertController
                                alertControllerWithTitle:title
                                message:message
                                preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                  style: UIAlertActionStyleCancel
                                  handler:nil];

    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
});

just edited your cancel button alert style....

Upvotes: 1

Sargis
Sargis

Reputation: 1334

Try this:

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                               style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * _Nonnull action) {
//your code here ...

                                                             }];

Upvotes: 1

Vamshi Krishna
Vamshi Krishna

Reputation: 989

Use this:

  UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                           style:UIAlertActionStyleCancel
                                                         handler:nil];

Change UIAlertActionStyleDefault to UIAlertActionStyleCancel

Upvotes: 1

KKRocks
KKRocks

Reputation: 8322

Try this :

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                  UIAlertActionStyleDefault
                                  handler:nil];

Upvotes: 0

Related Questions