2016-07-01 28 views
2

iOS的多行文本框下面是我的代碼的UITextField - 上AlertView

我加入的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]; 
+0

檢查此https://github.com/wimagguc/ios-custom-alertview – kb920

+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 
+0

那麼我們應該定製控制器來獲取多行中的文本? –

+0

http://stackoverflow.com/questions/6398519/objective-c-how-to-create-a-multi-line-uitextfield – user3182143

0

UITextFIeld是特定於一個線路。你不能有多行textfeild。如果你需要多線,你應該去UITextView。同樣,您不能將UITextView添加到警報控制器。您需要構建自定義警報視圖。

+0

我試圖與TextView的更換,但其造成一些錯誤 –

+0

你是對的。 TExtView不適用於UIAlertcontroller。您應該構建自定義的alertView。您不能將UIAlertController超出其範圍。 –

+0

但使用自定義的alertviews將有點花式,所以,我正在尋找一些本地 –