如何在警报视图IOS中创建两个文本字段

前端之家收集整理的这篇文章主要介绍了如何在警报视图IOS中创建两个文本字段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个包含两个uitextfields的alertview.
  1. method:
  2. //show alertview for file input
  3. - (IBAction)showAddFiles:(id)sender {
  4. UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Enter File Details"
  5. message:nil
  6. delegate:self
  7. cancelButtonTitle:@"Cancel"
  8. otherButtonTitles:@"Add",nil];
  9.  
  10.  
  11.  
  12. UITextField *textFieldDescription = [message textFieldAtIndex:0];
  13. textFieldDescription.placeholder = @"File Description : Ex. Acat Briefing";
  14. UITextField *textFieldFileName = [message textFieldAtIndex:1];
  15. textFieldFileName.placeholder = @"Exact File Name : Ex. acat.pdf";
  16.  
  17.  
  18. [message show];
  19. }
  20.  
  21.  
  22. //make sure file description is long enoguh
  23. - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
  24. {
  25. NSString *inputText = [[alertView textFieldAtIndex:0] text];
  26.  
  27. if( [inputText length] <= 15 && [inputText length] >= 4)
  28. {
  29. return YES;
  30. }
  31. else
  32. {
  33. return NO;
  34. }
  35. }
  36.  
  37. //handle add button
  38. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  39. {
  40. NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
  41. if([title isEqualToString:@"Add"])
  42. {
  43. UITextField *fileDescription = [alertView textFieldAtIndex:0];
  44. UITextField *fileName = [alertView textFieldAtIndex:1];
  45. NSLog(@"Desc: %@\nName: %@",fileDescription.text,fileName.text);
  46. }
  47. }

错误

*由于未捕获的异常’NSInvalidArgumentException’终止应用程序,原因:’textFieldIndex(0)超出了文本字段数组的范围’

为什么我会收到此错误,如何在警报视图中创建两个uitextfield?

=========工作解决方案===========
感谢下面的答案,当你只需要两个纯文本字段时

  1. //show alertview for file input
  2. - (IBAction)showAddFiles:(id)sender {
  3. UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Enter File Details"
  4. message:nil
  5. delegate:self
  6. cancelButtonTitle:@"Cancel"
  7. otherButtonTitles:@"Add",nil];
  8.  
  9.  
  10.  
  11. [message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
  12. UITextField *fileDescription = [message textFieldAtIndex:0];
  13. fileDescription.placeholder=@"Ex. acat.pdf";
  14. [[message textFieldAtIndex:1] setSecureTextEntry:NO];
  15. UITextField *fileName= [message textFieldAtIndex:1];
  16. fileName.placeholder=@"Ex. Acat Briefing";
  17.  
  18. [message show];
  19. }

解决方法

分配“消息”警报视图后.将其添加到您的代码中:
  1. [message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
  2. [[message textFieldAtIndex:1] setSecureTextEntry:NO];

这将使您的警报视图内部有两个文本字段.

猜你在找的iOS相关文章