swift
1import UIKit 2 3class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ 4 5 @IBOutlet weak var mytableView: UITableView! 6 var item = [String]() 7 8@IBAction func addlabel(_ sender: Any) { 9 10 alert() 11 12 } 13 14 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 15 return item.count 16 } 17 18 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 19 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 20 let todoLabel = item[indexPath.row] 21 cell.textLabel?.text = todoLabel 22 23 //ボタンについて 24 let button = UIButton() 25 button.backgroundColor = UIColor.blue 26 button.setTitle(" 追 加 ", for: .normal) 27 cell.contentView.addSubview(button) 28 29 button.translatesAutoresizingMaskIntoConstraints = false 30 cell.contentView.rightAnchor.constraint(equalTo: button.rightAnchor, constant: 12).isActive = true 31 // 中央にする 32 //button.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true 33 //丸みに対して 34 button.layer.cornerRadius = 10 35 button.layer.masksToBounds = true 36 37 cell.contentView.heightAnchor.constraint(equalTo: button.heightAnchor, multiplier: 1).isActive = true 38 39 //ラベルについて 40 var label = UILabel() 41 label.backgroundColor = UIColor.darkGray 42 label.text = "120×10" 43 //丸みに対して 44 label.layer.cornerRadius = 5 45 label.layer.masksToBounds = true 46 cell.contentView.addSubview(label) 47 48 label.translatesAutoresizingMaskIntoConstraints = false 49 cell.contentView.leftAnchor.constraint(equalTo: label.leftAnchor, constant: -6).isActive = true 50 cell.contentView.heightAnchor.constraint(equalTo: label.heightAnchor, multiplier: 1).isActive = true 51 52 //アラート 53 func alert(){ 54 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 55 56 // OKボタンの設定 57 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 58 (action:UIAlertAction!) -> Void in 59 60 // OKを押した時入力されていたテキストを表示 61 if let textFields = alert.textFields { 62 63 // アラートに含まれるすべてのテキストフィールドを調べる 64 for textField in textFields { 65 66 self.item.insert(textField.text!, at: 0) 67 self.label.text = textField.text! 68 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 69 print(textField.text!) 70 } 71 } 72 }) 73 alert.addAction(okAction) 74 75 // キャンセルボタンの設定 76 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 77 alert.addAction(cancelAction) 78 79 // テキストフィールドを追加 80 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 81 textField.placeholder = "テキスト" 82 }) 83 84 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 85 86 // アラートを画面に表示 87 self.present(alert, animated: true, completion: nil) 88 } 89 return cell 90 } 91 92 93 override func viewDidLoad() { 94 super.viewDidLoad() 95 96 } 97 98 99 100 override func didReceiveMemoryWarning() { 101 super.didReceiveMemoryWarning() 102 // Dispose of any resources that can be recreated. 103 } 104 105 106} 107
やりたい事
labelがセルに挿入されるときにアラートにて入力したものを反映させたい。
起きている問題
self.label.text = textField.text!の部分でlabelの部分でエラーが起きていてどう書いてもエラーが取れなくて困っています。
試しにcellForRowAt外にlabelのインスタンスを作ると外のインスタンスに対してはエラーが起きませんでした。
エラーメッセージ
Value of type 'ViewController' has no member 'label'
swift
1 //アラート 2 func alert(n: String){ 3 4 5 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 6 7 // OKボタンの設定 8 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 9 (action:UIAlertAction!) -> Void in 10 11 // OKを押した時入力されていたテキストを表示 12 if let textFields = alert.textFields { 13 14 // アラートに含まれるすべてのテキストフィールドを調べる 15 for textField in textFields { 16 17 self.item.insert(textField.text!, at: 0) 18 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 19 label.text = textField.text! 20 print(textField.text!) 21 } 22 } 23 }) 24 alert.addAction(okAction) 25 26 // キャンセルボタンの設定 27 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 28 alert.addAction(cancelAction) 29 30 // テキストフィールドを追加 31 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 32 textField.placeholder = "テキスト" 33 }) 34 35 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 36 37 // アラートを画面に表示 38 self.present(alert, animated: true, completion: nil) 39 } 40 41 alert(n: "kei") 42 print(alert)
関数の引数について
回答1件
あなたの回答
tips
プレビュー