###前提・実現したいこと
質問をご覧いただきまして、ありがとうございます。
UIAlertControllerに追加したUITextFieldから値を取り出す方法がわかりません。
下記のソースコードでAlertControllerにtextFieldを追加しておりまして、
OKボタンが押された時にtextFieldで入力した文字列を配列に追加したいのですが、
なかなか上手くいきません。
###ソースコード
import UIKit class ViewController: UIViewController, UIPopoverPresentationControllerDelegate { var goalArray: [String] = [] @IBOutlet weak var goalTable: UITableView! var toolBar: UIToolbar! override func viewDidLoad() { super.viewDidLoad() toolBar = UIToolbar() toolBar.sizeToFit() let toolBarButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "doneButton") toolBar.items = [toolBarButton] } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func tableView(tableView: UITableView, numberOfRowsInSection section: Int)->Int { return goalArray.count } func tableView(tableView: UITableView, cellForAtIndexPath indexpath: NSIndexPath)->UITableViewCell { let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell") cell.textLabel?.text = goalArray[indexpath.row] return cell } @IBAction func buttonTapped(sender: AnyObject) { let alertController = UIAlertController(title: "新しくリストを追加", message: "項目を入力して下さい。", preferredStyle: .Alert) let cancelAction = UIAlertAction(title: "Cancel", style: .Default){ action in NSLog("Cancel") } let otherAction = UIAlertAction(title: "Done", style: .Default){ action in NSLog("Done") } alertController.addAction(cancelAction) alertController.addAction(otherAction) //textFieldの追加 alertController.addTextFieldWithConfigurationHandler({(text: UITextField!) -> Void in text.tag = 0 text.placeholder = "リスト名を記入" }) alertController.addTextFieldWithConfigurationHandler({(text: UITextField!) -> Void in text.tag = 1 text.placeholder = "期限を設定(必須)" }) presentViewController(alertController, animated: true, completion: nil) } }
これまでに、defaultAction内に、textDidEndEditingメソッドを追加してみたりなどしてみたのですが、
思ったように動きません。
どのようにすれば良いのか、どなたかご教示いただければ幸いです。
質問の内容で、不足している情報があったら申し訳ございません。
回答1件
あなたの回答
tips
プレビュー