Iphone 输入文本时启用UIAlertView中的按钮-多个警报视图

Iphone 输入文本时启用UIAlertView中的按钮-多个警报视图,iphone,objective-c,ios,Iphone,Objective C,Ios,在我的应用程序中,当用户单击工具栏上的“保存”按钮时,系统会通过UIAlertView提示用户选择“另存为图像”或“另存为播放”来保存当前作品。当用户选择“另存为播放”时,系统会提示用户显示第二个UIAlertView,该视图还包含一个文本字段,供用户插入播放名称。我试图实现的是,当没有输入文本时,Ok按钮被禁用,当输入的文本长度为1或更多时,文件可以保存(使用archiver,这可以正确工作,因此这不是isue),Ok按钮被启用。下面列出的代码显示了两个警报视图,以及选择视图中的不同项目时发生

在我的应用程序中,当用户单击工具栏上的“保存”按钮时,系统会通过UIAlertView提示用户选择“另存为图像”或“另存为播放”来保存当前作品。当用户选择“另存为播放”时,系统会提示用户显示第二个UIAlertView,该视图还包含一个文本字段,供用户插入播放名称。我试图实现的是,当没有输入文本时,Ok按钮被禁用,当输入的文本长度为1或更多时,文件可以保存(使用archiver,这可以正确工作,因此这不是isue),Ok按钮被启用。下面列出的代码显示了两个警报视图,以及选择视图中的不同项目时发生的情况

- (IBAction)selectSaveType {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@""
                                                  message:@"Please Select an Option."
                                                 delegate:self
                                        cancelButtonTitle:@"Save Play"
                                        otherButtonTitles:@"Save to Photos", nil];
[message show];

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Save Play"])
{
    NSLog(@"Save Play was selected.");
    [self GetFileName];
}
else if([title isEqualToString:@"Save to Photos"])
{
    NSLog(@"Save To Photos was selected.");
    //here is where we need to find how to call saveDrawing.
    [self saveDrawing];

}
else if([title isEqualToString:@"Ok"])
{
    NSLog(@"OK selected");
    UITextField *fName= [alertView textFieldAtIndex:0];
    NSString *NameFile = fName.text;
    [self savePlay:NameFile];


}

}

-(void)savePlay:(NSMutableString *)fileName{
//code here to save via archive.
   NSArray *pathforsave = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentDirectory = [pathforsave objectAtIndex:0];
    //here we need to add the file extension onto the file name before we add the name to the path
   [fileName appendString:@".hmat"];
   NSString *strFile = [documentDirectory stringByAppendingPathComponent:fileName];
[NSKeyedArchiver archiveRootObject:matView.drawables toFile:strFile];

}
我一直在尝试使用下面的代码来处理这个问题,但是当第一个UIAlertView启动时(即请求选择一个播放-没有文本字段存在),下面的函数就会运行并崩溃应用程序,因为第一个警报视图中没有文本字段

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 1 )
{
    return YES;
}
else
{
    return NO;
}
}
当第一个警报触发时,alertViewShouldEnableFirstOtherButton被点击,然后我的应用程序在模拟器中崩溃。有人知道为什么会这样吗?有两件事我不太确定

第一个-为什么第二个警报视图上用于命名播放的Ok按钮的句柄在处理其他按钮的同一块中处理。既然它是一个单独的警报视图,它不应该在自己的块中吗

第二,为什么AlertViewshoulEnableFirstotherButton在还没有进入第二个alert视图时被点击,它会被调用并与第一个alert视图一起运行,这会使应用程序崩溃


感谢您的帮助,我对目标C还不熟悉。

对于您提供的任何警报视图,都将调用警报视图的委托方法。也就是说,此代码将崩溃,因为普通警报视图中不存在
textfieldatinex:0
。要解决此问题,您只需向委托方法添加一条if语句,以标识调用该操作的警报

编辑:不再按名称标识警报。代码现在检查代理发件人的样式

  - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    if (alertView.alertViewStyle == UIAlertViewStylePlainTextInput) {
        if([[[alertView textFieldAtIndex:0] text] length] >= 1 )
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }else{
        return YES;
    }
}

啊-有道理。我会试试这个,让你知道它是如何工作的。谢谢myTextAlertView-这应该是什么?我正在尝试处理的警报视图名为alertView-当我替换此alertView时,它将始终为alertView-我还尝试重命名alertView-但这不起作用。-在我把这个贴出来之前,我没有看到你的编辑,现在就试着编辑。