0

I have two view controllers on a Navigation stack. In the first view controller, I have a method that shows an alert to go to a website and navigate away from the app. If they click NO, nothing happens, if YES, then it opens the link. This all works. My problem is now if I want to call this method from the second view controller on the navigation stack, my first view controller gets dealloc'd, and the delegate never gets called. Here's some pseudo code of what is going on.

In SecondViewcontroller:

- (void)TargetSelectionPressed:(NSNotification *)notification {

    FirstViewController *dummyCtlr = nil;
    BOOL shouldPushToFistCtlr = NO;

    NSArray *controllers = [[NSArray alloc] initWithArray:[self.navigationController viewControllers]];

    for (UIViewController *ctlr in controllers) {
        if ([ctlr isKindOfClass:[FirstViewController class]]) {
            dummyCtlr = (FirstViewController *)ctlr;
            break;
        }
    }
    if (dummyCtlr == nil) {
        FirstViewController *gtc = [[[FirstViewController alloc] init] autorelease];
        dummyCtlr = gtc;
        shouldPushToFistCtlr = YES;
    }

    switch (targetSelection) {
        case TARGET_LOCATION:
            break;
        case TARGET_CONDITION: {
            if (shouldPushToGTC) {
                [self.navigationController pushViewController:dummyCtlr animated:YES];
            }  
            else {
                [self.navigationController popToViewController:dummyCtlr animated:YES];
            }
            break;
        }
        case TARGET_LINK: {
            [dummyCtlr BuildURL:link];
            break;
        }
        default:
            break;
    }
    [controllers release];
}

FirstViewcontroller

- (void)BuildURL:(NSString* )link {
    self.ExternalLink = link;     NSString *message = [[NSString alloc] initWithFormat:@"Would you like to open %@ in Safari?", link];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Open Safari" message:message delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    self.SafariAlertView = alert;
    self.SafariAlertView.delegate = self;
    [alert release];
    [message release];
}

- (void)dealloc {
    [safariAlertView release];
    self.SafariAlertView.delegate = nil;
    [super dealloc];
}
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
J W
  • 868
  • 1
  • 12
  • 25

2 Answers2

0

Check if *dummyCtlr is nil rather than if dummyCtlr is nil.

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
0

I found that if I made a property for the dummyCtlr, the object would stay around long enough for the delgate to be called.

J W
  • 868
  • 1
  • 12
  • 25