[英]Enable button in UIAlertView when text is inputted - multiple Alert Views
在我的应用程序中,当用户单击工具栏中的“保存”按钮时,将通过UIAlertView提示用户,他们希望通过选择保存为图像或保存为播放来保存当前工作。 当用户选择保存为播放时,然后提示第二个UIAlertView,它还有一个文本字段,供他们插入播放的名称。 我想要实现的是,当没有输入文本时,Ok按钮被禁用,当输入的文本长度为1或更多时,文件就可以保存(使用存档,这样可以正常工作这不是一个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被点击,然后我的应用程序在模拟器中崩溃。 有谁知道为什么会这样? 我不太确定的两件事
一个 - 为什么第二个警报视图上用于命名播放的“确定”按钮的句柄在处理其他按钮的同一个块中处理。 由于它是一个单独的警报视图,它不应该在它自己的块中吗?
二 - 为什么alertViewShouldEnableFirstOtherButton在尚未进入第二个警报视图时被点击,它被调用并运行第一个警报视图,这会使应用程序崩溃。
谢谢你的帮助,我是客观C的新手。
将为您提供的任何警报视图调用警报视图的委派方法。 据说这个代码会崩溃,因为textFieldAtIndex: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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.