0

我在导航堆栈上有两个视图控制器。在第一个视图控制器中,我有一个方法可以显示一个警报,让您访问网站并离开应用程序。如果他们单击“否”,则不会发生任何事情,如果“是”,则会打开链接。这一切都有效。我现在的问题是,如果我想从导航堆栈上的第二个视图控制器调用此方法,我的第一个视图控制器会被释放,并且委托永远不会被调用。这是正在发生的事情的一些伪代码。

在 SecondView 控制器中:

- (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];
}

第一视图控制器

- (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];
}
4

2 回答 2

0

检查 if *dummyCtlrisnil而不是 if dummyCtlris nil

于 2011-11-22T20:06:12.467 回答
0

我发现如果我为 dummyCtlr 创建一个属性,该对象将停留足够长的时间,以便调用 delgate。

于 2011-11-22T20:46:59.117 回答