UItextField - AlertView

时间:2016-07-01 12:44:34

标签: objective-c uitextfield uialertcontroller

这是我的代码

我将uitextfield添加到alertview,我需要的是将数据从数组中获取到具有多行的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];

2 个答案:

答案 0 :(得分:1)

我为你的问题尝试了解决方案。如果你使用带有textField的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

答案 1 :(得分:0)

UITextFIeld特定于一行。你不能有多行textfeild。如果你需要多行,你应该去UITextView。同样,您无法将UITextView添加到警报控制器。您需要构建自定义警报视图。