在iOS的AlertView中如何创建多行文本框(UItextField)?

3

这是我的代码

我正在向警报视图中添加uitextfield,我需要的是从数组中获取多行数据并使其可编辑的uitextfield。

 NSString *selected = [lineItemsArray objectAtIndex:indexPath.row];
UIAlertController *controller = [UIAlertController                                      alertControllerWithTitle:@"Notice" message:@"Selected"                                   preferredStyle:UIAlertControllerStyleAlert];
[controller addTextFieldWithConfigurationHandler:^(UITextField *linetextField)
 { 
     linetextField.text=selected;  
 }];
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"Okay"
                                                      style:UIAlertActionStyleDestructive
                                                    handler:^(UIAlertAction *action) {
                                                        [self.navigationController popViewControllerAnimated:YES];
                                                        NSLog(@"Dismiss button tapped!");
                                                    }];
[controller addAction:alertAction];
[self presentViewController:controller animated:YES completion:nil];
PackageLineItemViewController *pLineVC = [[PackageLineItemViewController alloc]init];
pLineVC.view.backgroundColor = [UIColor whiteColor];
[self.navigationController pushViewController:pLineVC animated:YES];
[self.view endEditing:YES];

请查看以下链接:https://github.com/wimagguc/ios-custom-alertview - kb920
我需要本地方法。 - Santhosh
2个回答

1

我尝试了你的问题的解决方案。如果你使用带有文本框的alertViewController,你不能设置多行。对于标签,你可以设置多行,但是对于文本框,你不能设置。

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>
{

  NSArray *arr;
}

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.

   arr = [NSArray arrayWithObjects:@"iOS",@"Android",@"Windows",nil];

 }

 - (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
 }

 - (IBAction)actionShowClickTextField:(id)sender
 {


  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                           message:@"Enter your text"
                                                                  preferredStyle:UIAlertControllerStyleAlert];

 [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

    textField.delegate = self;
    textField.text = @"";
    for(int i=0;i<[arr count];i++)
    {
        textField.text = [textField.text stringByAppendingString:[NSString stringWithFormat:@"%@,\n",[arr objectAtIndex:i]]];
    }

    NSLog(@"The textField text is - %@",textField.text);
}];


  [self presentViewController:alertController animated:YES completion:nil];

  UIAlertAction *actionAlert = [UIAlertAction actionWithTitle:@"Save"
                                                      style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction * action) {
                                                        // access text from text field
                                                        NSString *text = ((UITextField *)[alertController.textFields objectAtIndex:0]).text;
                                                        NSLog(@"The text is- %@",text);
                                                    }];
 actionAlert.enabled = YES;

 [alertController addAction:actionAlert];


}
@end

打印输出结果为:
The textField text is - iOS, Android, Windows

那么我们应该自定义控制器以获取多行文本框吗? - Santhosh
https://dev59.com/dmw15IYBdhLWcg3wy-vO - user3182143

0

UITextFIeld 是特定于一行的。您不能拥有多行文本框。如果您需要多行,则应选择 UITextView。同样,您不能将 UITextView 添加到警报控制器中。您需要构建自定义警报视图。


我尝试用TextView替换,但是出现了一些错误。 - Santhosh
你说得对。TextView 不能与 UIAlertcontroller 一起使用。你应该构建自定义的 alertView。你无法超出其范围定制 UIAlertController。 - Teja Nandamuri
但是使用自定义的警告视图会有点花哨,所以我正在寻找一些原生的东西。 - Santhosh
1
不幸的是,苹果没有提供内置的自定义警报。构建自定义警报并不是一项艰巨的任务。您只需以弹出动画的形式呈现UIView即可。您可以在xib中构建UIView,并像弹出窗口一样呈现它。 - Teja Nandamuri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接