0

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

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

这是代码

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

}

4

2 回答 2

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:并给出适当的CGRectivar 而不是sender

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

于 2012-10-15T05:16:29.977 回答
1

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

于 2012-10-15T05:01:34.240 回答