2017-07-27 51 views
1

概述 - 我制作了一个flashcard应用程序。我已经到了用户可以通过一系列图像左右滑动的地步。图像被分成11个不同的组,并且所有的组合都可以添加一个用户可以滑过的最终数组(下面的代码)。UIswitch在单独视图控制器中激活或停用图像阵列

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]) 

    // Do any additional setup after loading the 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 is 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 is in range 

      if imageIndex > imageList.count - 1 { 

       imageIndex = 0 

      } 

      imgPhoto.image = UIImage(named: imageList[imageIndex]) 

     default: 
      break //stops the code/codes nothing. 
     } 
    } 
} 
} 

现在我正在设置页面(下图)。上面代码中的11个单词列表中的每一个都有1个开关。最上面的开关会控制列表1中,第二开关将控制列表2等等

enter image description here

的问题 - 是我想将功能添加到每个交换机。当开关处于关闭位置时,与其关联的图像组不应包含在最终阵列中,并且在用户滑过闪卡时不会显示。到目前为止,我的设置页面的代码如下所示。我尝试过试验不同的代码,比如将开关连接到ViewController并添加覆盖函数,但我不确定此时该去哪里。

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 look 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 any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the 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 the 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 to support conditional editing of the table view. 
override func tableView(_ tableView: UITableView, can 

任何想法如何解决这个问题将不胜感激。谢谢

enter image description here

+0

我无法在代码中看到被设置的UISwitch的委托/操作目标,您需要这样做否则'switchAction'方法将永远不会被调用。你在代码中的其他任何地方做过吗?如果你在该方法中设置了一个断点,它是否曾被称为? – Mrwerdo

+0

我想我没有设置。设置页面是一个分割视图控制器,所以还有其他类与分割视图相关联,但我没有将切换操作方法放在除上述之外的任何其他类中。 –

回答

0

你错过了一些零件到您的程序,使这项工作。它们是:

  1. 设置UISwitch控件的目标方法。
  2. 设置UISwitch控件的标签号。
  3. 刷新SecondViewController中显示的图像。

要设置UISwitch控制的目标的方法,你就需要调用addTarget(_:action:for:)每个交换机上,通过引用,你有MasterViewController。如果你这样做,那么我建议在Interface Builder中为你的UISwitch控件设置一个标签。例如,假设你UISwitch具有1标记:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

    // Configure the cell... 
    let word = self.words[indexPath.row] 
    cell.textLabel?.text = word.name 
    if let switchControl = cell.viewWithTag(1) as UISwitch { 
     switchControl.addTarget(self, action: #selector(switchAction(_:)), for: .valueChanged) 
    } 

    return cell 
} 

对于点3中,数据在MasterViewController比在SecondViewController不同。您需要获得对SecondViewController的引用,然后更新那里的数据,并告诉它重新显示所选图像。有关如何从分割视图控制器访问对等视图控制器,请参阅this question for details

+0

谢谢。但是,闪存卡不会显示在“详细信息”视图控制器中。我将在那里显示其他信息。我为故事板添加了一张照片,以便您可以看到该应用的布局。与猫头鹰的场景是开幕画面,然后是闪卡(具有鳄鱼图像的场景,这是SecondViewController)的延续。在右边你可以看到我的分割视图控制器,这是我正在处理的设置页面。所以我想将MainViewController(设置页面)的信息传递给SecondViewController(鳄鱼场景) –

相关问题