ios - SWIFT 3 - I need to create a way to select a relation to an item and then save it -


i need create way selected value table view relationship text entered in uitextfield , save when clicking save button. here way organized views , code:

views , need do

data model

this view controller add view.

import uikit  class adddisciplinasviewcontroller: uiviewcontroller, uitextviewdelegate {      let context = (uiapplication.shared.delegate as! appdelegate).persistentcontainer.viewcontext     var materias: [materia] = []     var selectedindex: int!      var filtereddata: [materia] = []      var teste = ["teste","teste2"]      func fetchdata() {          {             materias = try context.fetch(materia.fetchrequest())              filtereddata = materias          } catch {             print("couldn't fetch data")         }      }      override func viewwillappear(_ animated: bool) {         fetchdata()     }      var selectedmat: string?      @iboutlet var itementrytextview: uitextview?      @ibaction func cancel(_ sender: any) {         dismiss(animated: true, completion: nil)     }     @ibaction func savecontact(_ sender: any) {          if (itementrytextview?.text.isempty)! || itementrytextview?.text == "digite o nome da matéria..."{             print("sem dados")              let alert = uialertcontroller(title: "por favor, escreva algo!", message: "sua matéria ficou vazia.", preferredstyle: .alert)             alert.addaction(uialertaction(title: "okay", style: .default) { action in              })              self.present(alert, animated: true, completion: nil)          } else {              let context = (uiapplication.shared.delegate as! appdelegate).persistentcontainer.viewcontext             let newentry = disciplina(context: context)             newentry.name = itementrytextview?.text!               (uiapplication.shared.delegate as! appdelegate).savecontext()              dismiss(animated: true, completion: nil)          }      }      override func viewdidload() {         super.viewdidload()          itementrytextview?.delegate = self          // additional setup after loading view.     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }       func textviewdidbeginediting(_ textview: uitextview) {         textview.text = ""         textview.textcolor = uicolor.white     }      func textview(_ textview: uitextview, shouldchangetextin range: nsrange, replacementtext text: string) -> bool {         if (text == "\n") {             textview.resignfirstresponder()             return false         }         return true     } }   extension adddisciplinasviewcontroller: materiapiclerdelegate {      func didselectmateria(materia: materia) {         //     }  } 

this view controller table view entities need relate.

import uikit  protocol materiapiclerdelegate: class {     func didselectmateria(materia: materia) }  class selectmateriaviewcontroller : uitableviewcontroller {      let context = (uiapplication.shared.delegate as! appdelegate).persistentcontainer.viewcontext     var items: [materia] = []      override func viewdidload() {         super.viewdidload()           self.tableview.estimatedrowheight = 10         self.tableview.rowheight = uitableviewautomaticdimension      }      override func viewwillappear(_ animated: bool) {         fetchdata()     }      func fetchdata() {          {             items = try context.fetch(materia.fetchrequest())             dispatchqueue.main.async {                 self.tableview.reloaddata()             }         } catch {             print("couldn't fetch data")         }      }  }  extension selectmateriaviewcontroller {      override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {         let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath) uitableviewcell          cell.textlabel?.text = items[indexpath.row].name          return cell     }      override func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {         return items.count     }      override func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) {          //     }   } 

so it's study tracking app user adds subject (materia) in views , adds topic (disciplina) , select patent subject in view. can't figure out how subject selected table , save entry , relationship between these 2 items.

how can this?


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -