ios - UIswitch to activate or deactivate an image array in seperate view controller -


overview - making flashcard app. have gotten point user can swipe left , right through array of images. images split 11 different groups , of groups add 1 final array user can swipe through(code below).

import uikit  class secondviewcontroller: uiviewcontroller , uigesturerecognizerdelegate  {  @ibaction func home(_ sender: any) {     performsegue(withidentifier: "home", sender: self) }  @iboutlet weak var imgphoto: uiimageview!  struct list {     let words: [string]     var active: bool }  let list1 = list(words:["lake", "lamb", "lamp", "lark", "leaf", "leash", "left", "leg", "lime", "lion", "lips", "list", "lock", "log", "look", "love", "lunch"], active: true)  let list2 = list(words: ["ladder", "ladybug", "laughing", "lawnmower", "lemon", "leopard", "leprechaun", "letters", "licking", "lifesaver", "lifting", "lightbulb", "lightning", "listen", "llama"], active: true)  let list3 = list(words: ["alligator", "balance", "ballerina", "balloon", "bowling", "cello", "colors", "curlyhair", "dollar", "dolphin", "elephant", "eyelashes", "gasoline", "goalie", "hula", "jellyfish", "olive", "pillow", "pilot", "polarbear", "rollerskate", "ruler", "silly", "telephone", "television", "tulip", "umbrella", "valentine", "violin", "xylophone", "yellow"], active: true)  let list4 = list(words: ["apple", "ball", "bell", "bubble", "castle", "fall", "fishbowl", "girl", "owl", "pail", "peel", "pool", "smile", "whale", "wheel"], active: true)  let list5 = list(words: ["planet", "plank", "plant", "plate", "play", "plum", "plumber", "plus"], active: true)  let list6 = list(words: ["black", "blanket", "blender", "blocks", "blond", "blood", "blow", "blue"], active: true)  let list7 = list(words: ["flag", "flipflop", "float", "floor", "flower", "fluffy", "flute", "fly"], active: true)  let list8 = list(words: ["glacier", "glad", "glasses", "glide", "glitter", "globe", "glove", "glue"], active: true)  let list9 = list(words: ["clam", "clamp", "clap", "claw", "clean", "climb", "clip", "cloud"], active: true)  let list10 = list(words:["sled", "sleep", "sleeves", "slice", "slide", "slime", "slip", "slow"], active: true)  let list11 = list(words: ["belt", "cold", "dolphin", "elf", "golf", "melt", "milk", "shelf"], active: true)  var imageindex: int = 0  var imagelist: [string] {      let wordlists = [list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11]        let active = wordlists.reduce([]) { (result:[string], list:list) in         if list.active {             return result + list.words          } else {             return result         }     }      return active  }     override func viewdidload() {     super.viewdidload()      imgphoto.image = uiimage(named: imagelist[imageindex])      // additional setup after loading view.     imgphoto.isuserinteractionenabled = true      let leftswipe = uiswipegesturerecognizer(target: self, action: #selector(swiped(gesture:)))     leftswipe.cancelstouchesinview = false      let rightswipe = uiswipegesturerecognizer(target: self, action: #selector(swiped(gesture:)))     rightswipe.cancelstouchesinview = false      leftswipe.direction = .left     rightswipe.direction = .right      view.addgesturerecognizer(leftswipe)     view.addgesturerecognizer(rightswipe)  }  func swiped(gesture: uigesturerecognizer) {      if let swipegesture = gesture as? uiswipegesturerecognizer {          switch swipegesture.direction {          case uiswipegesturerecognizerdirection.right :             print("user swiped right")              // decrease index first              imageindex -= 1              // check if index in range              if imageindex < 0 {                  imageindex = imagelist.count - 1              }              imgphoto.image = uiimage(named: imagelist[imageindex])          case uiswipegesturerecognizerdirection.left:             print("user swiped left")              // increase index first              imageindex += 1              // check if index in range              if imageindex > imagelist.count - 1 {                  imageindex = 0              }              imgphoto.image = uiimage(named: imagelist[imageindex])          default:             break //stops code/codes nothing.         }     } } } 

now - working on settings page(image below). there 1 switch each of 11 lists of words in code above. top switch control list1, second switch control list2 etc...

enter image description here

the problem - want add functionality each switch. when switch in off position, group of images associated should not included in final array , not displayed when user swiping through flashcards. code settings page far below. have tried experimenting different bits of code such connecting switch viewcontroller , adding override func not sure go @ point.

import uikit  protocol wordselectiondelegate: class { func wordselected(newword: word) }  class masterviewcontroller: uitableviewcontroller { var words = [word]()  weak var delegate: wordselectiondelegate?  override func prepare(for segue: uistoryboardsegue, sender: secondviewcontroller) {     if let vc = segue.destination as? masterviewcontroller {         vc.wordlists = wordlists     } }   @ibaction func switchaction(_ sender: uiswitch) {      wordlist[sender.tag].active = rollintoloanswitch.ison }    override func viewdidload() {     super.viewdidload()    } required init(coder adecoder: nscoder) {     super.init(coder: adecoder)!      self.words.append(word(name: "initial /l/ 1 syllable", description: "lake lamb lamp lark leaf leash left leg lime lion lips list lock log love lunch"))      self.words.append(word(name: "initial /l/ multisyllabic", description: ""))      self.words.append(word(name: "intersyllabic /l/", description: ""))      self.words.append(word(name: "final /l/", description: ""))      self.words.append(word(name: "initial /pl/", description: ""))      self.words.append(word(name: "initial /bl/", description: ""))      self.words.append(word(name: "initial /fl/", description: ""))      self.words.append(word(name: "initial /gl/", description: ""))      self.words.append(word(name: "initial /kl/", description: ""))      self.words.append(word(name: "initial /sl/", description: ""))      self.words.append(word(name: "final /l/ clusters", description: ""))   }   override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  // mark: - table view data source  override func numberofsections(in tableview: uitableview) -> int {     // #warning incomplete implementation, return number of sections     return 1 }  override func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {     // #warning incomplete implementation, return number of rows     return self.words.count }   override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath)       // configure cell...     let word = self.words[indexpath.row]     cell.textlabel?.text = word.name     return cell }  override  func tableview(_ tableview: uitableview, didselectrowat     indexpath: indexpath) {     let selectedmonster = self.words[indexpath.row]     self.delegate?.wordselected(newword: selectedmonster)     if let detail = self.delegate as? detail {         splitviewcontroller?.showdetailviewcontroller(detail, sender: nil)     }  } /* // override support conditional editing of table view. override func tableview(_ tableview: uitableview, can 

any thoughts on how go solving problem appreciated . thank you

enter image description here

you're missing number of parts program make work. are:

  1. setting target method of uiswitch control.
  2. setting tag number of uiswitch control.
  3. refreshing presented images in secondviewcontroller.

to set target method of uiswitch control, you'll need call addtarget(_:action:for:) on each switch, passing reference masterviewcontroller have. if this, i'd recommend setting tag uiswitch control in interface builder. example, assuming uiswitch had tag of 1:

override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath)       // configure cell...     let word = self.words[indexpath.row]     cell.textlabel?.text = word.name     if let switchcontrol = cell.viewwithtag(1) uiswitch {         switchcontrol.addtarget(self, action: #selector(switchaction(_:)), for: .valuechanged)     }      return cell } 

for point 3, data in masterviewcontroller different in secondviewcontroller. need obtain reference secondviewcontroller , update data there, , tell redisplay selected images. see this question details on how access peer view controller split view controller.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -