2011-11-24 48 views
1

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

现在我用两个UIAlertView将它缩小到这个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

任何想法我怎么能再次显示相同的对话框一个非常基本的应用程序?谢谢。

+0

你在哪里显示您alertView ......当你告诉一个alertView和用户点击任何按钮clickedButtonAtIndex只会被称为.. – Krishnabhadra

+0

待办事项你每次都必须使用完全相同的UIAlertView?另一种可能是创建两个函数来重新创建相应的对话框。 –

+0

@TriPhoenix:我之前就有过,它工作。但我真的想重复使用对象,而不是一直分配新对象。 – oyophant

回答

2

你应该实现didDismissWithButtonIndex委托方法:

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

已接受。非常感谢。有效! – oyophant

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]; 
    } 
} 
+0

我试过这个。不幸的是它没有奏效。 – oyophant