2017-08-28 9 views
-1

2つのTextFieldを表示する最初のView Controller(FirstView Controller)と、Labelを持つTableViewCellを含む1つのTableViewがあります。私は、1つのラベルを含むTableViewCellでTableViewを含む2番目のView Controller(TypeViewController)を持っています。 FirstViewControllerでTableViewCellのラベルのテキストを変更します

選択したテキストとTableViewCellを選択 - >バックFirstViewControllerに行くと、テキストの代わりに - TableViewCellを(TypeViewControllerに私は動的にセルのリスト、そのロードを持っている)を選択TypeViewController ON>ビューTypeViewControllerに行く

を選択すると、TypeViewControllerで選択されたテキストが表示されます。

FirstViewControllerのTableViewCellのLabelのテキストをTypeViewControllerのテキストに変更するにはどうすればよいですか?

最初のビューコントローラ

import UIKit 

class FirstViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate { 

    var animalTypeName: String?; 

    var selectedTypeId: UInt?; 
    var selectedTypeName: String?; 

    override func viewDidLoad() { 
     super.viewDidLoad(); 
    } 

    // MARK: TableView && TableViewCell Methods 
    // Row display. 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1; 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FirstTableViewCell; 
     animalTypeName = "Choose"; 
     cell.typeName.text = animalTypeName; 

     return cell; 
    } 

    //MARK: - Navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

     super.prepare(for: segue, sender: sender) 

     switch(segue.identifier ?? "") { 
      case "ShowType": 
       guard segue.destination is TypeViewController else { 
       fatalError("Unexpected destination: \(segue.destination)"); 
       } 
      default: 
       fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))"); 
     } 
    } 

    //MARK: Actions 
    @IBAction func unwindToCreateMedicamentController(sender: UIStoryboardSegue) { 
     if let typeViewController = sender.source as? TypeViewController, let type = typeViewController.type { 

      selectedTypeName = type.name; 
      selectedTypeId = type.animalTypeId; 
      animalTypeName = selectedTypeName; 
     } 
    } 
} 

viewDidLoad方法あなたはNEのTypeViewController

import UIKit 

class TypeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var tableView: UITableView! 

    var typeList: [AnimalType]?; 
    var type: AnimalType?; 
    let typeController = AnimalTypeController(); 

    override func viewDidLoad() { 
     super.viewDidLoad(); 
     // Call method to get array of AnimalType items. 
     getArrayOfTypeItems(); 
    } 

    //MARK: - Table view data source 
    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1; 
    } 

    // Row display. 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return typeList?.count ?? 0; 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    // Table view cells are reused and should be dequeued using a cell identifier. 
    let cellIdentifier = "TypeTVCell"; 

    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TypeTableViewCell else { 
     fatalError("The dequeued cell is not an instance of TypeTVCell."); 
    } 

     let typeItem = typeList?[indexPath.row]; 
     cell.nameOfType.text = typeItem!.name; 

     return cell; 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

     super.prepare(for: segue, sender: sender) 

     switch(segue.identifier ?? "") { 
     case "ChooseType": 
      guard segue.destination is CreateMedicamentController else { 
       fatalError("Unexpected destination: \(segue.destination)"); 
      } 

      guard let selectedTypeCell = sender as? TypeTableViewCell else { 
       fatalError("Unexpected sender: \(String(describing: sender))"); 
      } 

      guard let indexPath = tableView.indexPath(for: selectedTypeCell) else { 
       fatalError("The selected cell is not being displayed by the table"); 
     } 
      let selectedType = typeList?[indexPath.row]; 
      type = selectedType; 

     default: 
      fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))") 
     } 
    } 
} 

This`s an example of my views

+0

unwindToCreateMedicamentController(sender: UIStoryboardSegue)に追加FirstViewController

  • @IBOutlet weak var tableView: UITableView!を追加します。 –

  • +0

    これは、SOに関する最も一般的なiOSの質問の1つである必要があります。 https://medium.com/@jamesrochabrun/implementing-delegates-in-swift-step-by-step-d3211cbac3ef – toddg

    答えて

    0
    class FirstViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate { 
    
        var animalTypeName: String?; 
    
        var selectedTypeId: UInt?; 
        var selectedTypeName: String?; 
    
        @IBOutlet weak var tableView: UITableView! 
    
        override func viewDidLoad() { 
         super.viewDidLoad(); 
         animalTypeName = "Choose"; 
        } 
    
        // MARK: TableView && TableViewCell Methods 
        // Row display. 
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
         return 1; 
        } 
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    
         let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FirstTableViewCell; 
         cell.typeName.text = animalTypeName; 
    
         return cell; 
        } 
    
        //MARK: - Navigation 
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    
         super.prepare(for: segue, sender: sender) 
    
         switch(segue.identifier ?? "") { 
          case "ShowType": 
           guard segue.destination is TypeViewController else { 
            fatalError("Unexpected destination: \(segue.destination)"); 
           } 
          default: 
           fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))"); 
         } 
        } 
    
        //MARK: Actions 
        @IBAction func unwindToCreateMedicamentController(sender: UIStoryboardSegue) { 
         if let typeViewController = sender.source as? TypeViewController, let type = typeViewController.type { 
    
          selectedTypeName = type.name; 
          selectedTypeId = type.animalTypeId; 
          animalTypeName = selectedTypeName; 
    
          tableView.reloadData(); 
         } 
        } 
    } 
    
    1. 移動animalTypeName = "Choose";:edはfolowingコードを追加します。
    2. あなたがデリゲートを設定する必要がtableView.reloadData()
    1

    viewDidLoad

    tableView.dataSource = self 
    tableView.delegate = self 
    
    関連する問題