UIAlertView显示比我预期的要晚

时间:2012-10-15 04:49:29

标签: objective-c ios5 uialertview

我正在使用UIAlertView在下一个模式视图之前输入用户名和密码凭据,这样可以访问其帐户中的文件。

然而,当我调试时,我发现在alertView出现之前调用了模态视图的“ViewDidLoad”方法,因此程序崩溃,因为视图无法获取用户名和密码。

这是代码

-(IBAction) openShareFile:(id)sender
{
 if (!self.shareFileBrowser) {
    self.shareFileBrowser=[[ShareFileBrowserViewController alloc]   initWithNibName:@"ShareFileBrowserViewController" bundle:nil];
    self.alertView = [[UIAlertView alloc] initWithTitle:@"Verify your Credentials" message:@"Enter  Username & Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    self.alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    [self.alertView show];

    self.shareFileBrowser.domain=@"fidomoose";
    self.shareFileBrowser.delegate=self;
    self.shareFileBrowser.title=@"ShareFile Browser";
    self.navController= [[UINavigationController alloc] initWithRootViewController:self.shareFileBrowser];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.popController = [[UIPopoverController alloc] initWithContentViewController:self.navController];
    }
}

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    if ([self.popController isPopoverVisible]) {
        [self.popController dismissPopoverAnimated:YES];
    } else {
        [self.popController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}
else {
    [self presentModalViewController:self.navController animated:YES];
}

}

2 个答案:

答案 0 :(得分:2)

只需将您的视图的代码放在UIAlertView的委托方法中,例如关注

-(IBAction) openShareFile:(id)sender
{
    if (!self.shareFileBrowser) {

        self.alertView = [[UIAlertView alloc] initWithTitle:@"Verify your Credentials" message:@"Enter  Username & Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
        //Edit Two line inserted

        self.alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
        [self.alertView show];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex)
    {
        self.shareFileBrowser=[[ShareFileBrowserViewController alloc]   initWithNibName:@"ShareFileBrowserViewController" bundle:nil]; 
        //Edit two line deleted

        self.shareFileBrowser.domain=@"fidomoose";
        self.shareFileBrowser.delegate=self;
        self.shareFileBrowser.title=@"ShareFile Browser";
        self.navController= [[UINavigationController alloc] initWithRootViewController:self.shareFileBrowser];
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            self.popController = [[UIPopoverController alloc] initWithContentViewController:self.navController];
        }
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            if ([self.popController isPopoverVisible]) {
                [self.popController dismissPopoverAnimated:YES];
            } else {
                [self.popController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
            }
        }
        else {
            [self presentModalViewController:self.navController animated:YES];
        }
    }
}

编辑:1

只需使用方法presentPopoverFromRect:代替presentPopoverFromBarButtonItem:,并提供适当的CGRect ivar而不是sender

这肯定会解决你的问题:)

答案 1 :(得分:1)

您应该更改您的应用结构,以便在调用警报视图的DismissWithClickedButtonIndex:animated:之前不会实例化(或推送)模态视图。