jerry
jerry

Reputation: 274

UIAlertView shows up late than i expect

I am using a UIAlertView for entering username and password credentials before the next modal view, that gives him access to files in his account, appears.

However, when I debug, I came to know that the "ViewDidLoad" method of the modal view is called before the alertView shows up, and hence the program crashes as the view is not able to get username and password.

Here is the code

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

}

Upvotes: 0

Views: 242

Answers (2)

The iOSDev
The iOSDev

Reputation: 5267

Just put your code for init of your view in the delegate method of UIAlertView like follow

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

EDIT:1

Just use the method presentPopoverFromRect: instead of presentPopoverFromBarButtonItem: and give proper CGRect ivar instead of sender

This will definitely solve your problem :)

Upvotes: 2

rdelmar
rdelmar

Reputation: 104082

You should change your app structure so that the modal view isn't instantiated (or pushed) until the alert view's DismissWithClickedButtonIndex:animated: is called..

Upvotes: 1

Related Questions