再次从iOS自己的委托中打开UIAlertView失败

时间:2011-11-24 11:34:43

标签: objective-c ios cocoa-touch modal-dialog uialertview

我有一个iPhone应用程序打开一个带有UITextInput字段和按钮的UIAlertView。当按下按钮时,委托方法应该对输入进行验证,并在成功时继续执行应用程序,或者在失败时再次打开相同的UIAlertView。

现在我用两个UIAlertViews将它减少到这个litte测试类。总是可以打开另一个视图,但是当我按下按钮时,它会询问屏幕是否为空。

#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

编辑:这是AppDelegate。它现在是一个非常基本的应用程序,没有任何视图控制器

#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

我有什么想法可以再次显示相同的对话框?感谢。

2 个答案:

答案 0 :(得分:2)

您应该实施didDismissWithButtonIndex委托方法:

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

答案 1 :(得分:0)

您需要重新实施- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex:委托方法。

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