3

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];
Ivan Genchev
  • 2,746
  • 14
  • 25
Santhosh
  • 525
  • 5
  • 13

2 Answers2

1

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
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

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.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • i tried replacing with textview but its causing some error – Santhosh Jul 01 '16 at 13:11
  • You are right. TExtView doesnt work with UIAlertcontroller. You should build custom alertView. You cannot custmoize UIAlertController beyond its scope. – Teja Nandamuri Jul 01 '16 at 13:12
  • but using customised alertviews will be bit fancy so ,Im searching for something which is native – Santhosh Jul 01 '16 at 13:20
  • 1
    Unfortunately apple doesnt provide built in customised alerts. Building the customnised alerts is not a tough task. YOu will be simply presenting a UIView in a pop-up animation. You can build the UIView in xib , and present it like a pop up. – Teja Nandamuri Jul 01 '16 at 13:22