Santhosh
Santhosh

Reputation: 525

UItextField - iOS multiple line textfield on AlertView

Here is my code

I am adding uitextfield to alertview , what I need is to get the data from the array to uitextfield with multiple lines and should be editable.

 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];

Upvotes: 3

Views: 1206

Answers (2)

user3182143
user3182143

Reputation: 9599

I tried the solution for your question.If you use alertViewController with textField you can't set multiple line.For label you can set multiple line but for textfield you can't set.

#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 printed output results are

The textField text is - iOS, Android, Windows

Upvotes: 1

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

UITextFIeld is specific to one line. you cannot have multi-line textfeild. IF you need multi line , you should go for UITextView. Again you cannot add UITextView to the alert controller. You need to build a custom alert view.

Upvotes: 0

Related Questions