oyophant
oyophant

Reputation: 1304

Open an UIAlertView again from its own delegate fails in iOS

I have an iPhone App that opens an UIAlertView with an UITextInput field and a button. When the button gets pressed the delegate method should do a validation of the input and on success proceed app execution or open the same UIAlertView again on failure.

Now I reduced it to this litte test class with two UIAlertViews. It is always possible to open the other view but when I hit the button it asks for the screen remains empty.

#import "Yesorno.h"

@implementation Yesorno
@synthesize promptYES, promptNO;

- (id)init {
    self = [super init];
    if (self) {
        promptYES = [[UIAlertView alloc] init];
        [promptYES setDelegate:self];
        [promptYES setTitle:@"Press YES"];
        [promptYES addButtonWithTitle:@"YES"];
        [promptYES addButtonWithTitle:@"NO"];
        promptNO = [[UIAlertView alloc] init];
        [promptNO setDelegate:self];
        [promptNO setTitle:@"Press NO!"];
        [promptNO addButtonWithTitle:@"YES"];
        [promptNO addButtonWithTitle:@"NO"];
    }
    return self;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0)
        [promptYES show];
    else
        [promptNO show];
}

@end

Edit: here is the AppDelegate. It is now really a very basic application without any view controller

#import "AppDelegate.h"
#import "Yesorno.h"

@implementation AppDelegate

@synthesize window, yesorno;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    yesorno = [[Yesorno alloc] init];
    [yesorno.promptYES show];
    return YES;
}

@end

Any ideas how I could show the same dialog again? Thanks.

Upvotes: 1

Views: 572

Answers (2)

Evgeny Aleksandrov
Evgeny Aleksandrov

Reputation: 779

You should implement didDismissWithButtonIndex delegate method:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0)
        [promptYES show];
    else
        [promptNO show];
}

Upvotes: 2

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31730

You need to reimplement your - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex: delegate method.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
     if(alertView == promptYES)
     {
        if (buttonIndex == 0)
          [promptYES show];
     }
     else if(alertView == promptNO)
     {
         if (buttonIndex == 0)
          [promptNO show];
     }
}

Upvotes: 0

Related Questions