设置页面来添加或减less不同的图像数组。 用swift和xcode编码

我正在尝试制作一个简单的闪卡应用程序。

概述 – 为了给你的应用程序的基本运行,所有的flashcards包含一个具有字母l的对象。 有时这个单词以字母l开始,有时候会有双重字母,有时这个单词以pl或者kl或者sl等等开头。

太远了 – 我已经做了应用程序,所以有一个主页,从主页按下Go,然后它需要你也是第二个视图控制器。 从那里你可以通过闪存卡左右滑动。 我通过创build一个包含所有图像的数组,然后添加滑动手势(下面的代码)

// // SecondViewController.swift // firstapp // // Created by Anthony Rubin on 6/20/17. // Copyright © 2017 rubin. All rights reserved. // import UIKit class SecondViewController: UIViewController , UIGestureRecognizerDelegate { @IBAction func home(_ sender: Any) { performSegue(withIdentifier: "home", sender: self) } @IBOutlet weak var imgPhoto: UIImageView! var imageList:[String] = ["alligator", "apple", "balance", "ball", "ballerina", "balloon", "bell", "belt", "black", "blanket", "blender", "blocks", "blond", "blood", "blow", "blue", "bowling", "bubble", "bully", "calendar", "castle", "cello", "clam", "clamp", "clap", "claw", "clean", "climb", "clip", "cloud", "cold", "colors", "crawl", "curlyhair", "dollar", "dolphin", "elephant", "elf", "eyelashes", "fall", "fishbowl", "flag", "flipflop", "float", "floor", "flower", "fluffy", "flute", "fly", "gasoline", "girl", "glacier", "glad", "glasses", "glide", "glitter", "globe", "glove", "glue", "goalie", "golf", "hula", "jellyfish", "ladder", "ladybug", "lake", "lamb", "lamp", "lark", "laughing", "lawnmower", "leaf", "leash", "left", "leg", "lemon", "leopard", "leprechaun", "letters", "licking", "lifesaver", "lifting", "lightbulb", "lightning", "lime", "lion", "lips", "list", "listen", "llama", "lock", "log", "look", "love", "lunch", "melt", "milk", "olive", "owl", "pail", "peel", "pillow", "pilot", "planet", "plank", "plant", "plate", "play", "plum", "plumber", "plus", "polarbear", "pool", "rollerskate", "ruler", "shelf", "silly", "sled", "sleep", "sleeves", "slice", "slide", "slime", "slip", "slow", "smile", "telephone", "television", "tulip", "umbrella", "valentine", "violin", "whale", "wheel", "xylophone", "yellow"] let maxImages = 135 var imageIndex: NSInteger = 0 override func viewDidLoad() { super.viewDidLoad() // 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 = maxImages } 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 > maxImages { imageIndex = 0 } imgPhoto.image = UIImage(named: imageList[imageIndex]) default: break //stops the code/codes nothing. } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ 

问题 – 我想创build一个设置页面。 这个设置页面将为每个单词组设置一个基本的开关。 所以我们可以说,我不想要任何带有首字母l的单词,我也会把按键也转到closures的位置,然后当我滑过这些图像时,将不会有最初的字母l的闪卡。

设置页面

我的想法 – 我想,我将不得不连接每个开关,以及它相应的单词的数组,然后编码,如果,然后声明每个button。 然后将所有数组一起添加。 但我不知道如何开始这样做。 我已经做了一个表视图与所有不同的开关,但还没有添加任何function。 我也不知道如何将信息从我的表视图发送到第二个视图控制器。

我知道这是很多问在一个问题,但任何帮助将不胜感激。 谢谢

这里有很多,但你似乎开了个好头。

如果单词和类别相当固定,那么我要做的第一件事就是将单词列表分成不同的类别,然后可以使用以下方法组合您想要的图像:

 let list1 = ["Bat", "Cow"] let list2 = ["Frog", "Rabbit"] let list3 = ["Parrot", "Tiger"] var imageList: [String] { return list1+list2 } 

您可以保留一个activeLists数组,然后使用reduce函数返回最终数组:

 var activeLists = [list1, list2] var imageList: [String] { return activeLists.reduce([], {$0 + $1}) } 

但是,您可能最终需要一个比简单的string数组更容易pipe理的数据源来存储信息:

 struct List { let words: [String] var active: Bool } 

那么你可以有:

 let list1 = List(words: ["Bat", "Cow"], active: true) let list2 = List(words: ["Frog", "Rabbit"], active: true) let list3 = List(words: ["Parrot", "Tiger"], active: true) var wordLists = [list1, list2, list3] var imageList: [String] { let active = wordLists.reduce([]) { (result:[String], list:List) in if list.active { return result + list.words } else { return result } } return active } 

注:我不会使用maxImages作为一个常量,因为这将需要改变,你可以使用imageList.count来代替。

关于发送信息,有几种方法可以做到这一点。 一种方法是使用prepareForSegue将信息发送到新的viewController。 像这样的东西:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let vc = segue.destination as? SettingsViewController { vc.wordLists = wordLists } } 

然后在设置中,您可以切换单个单词列表:

 wordLists[1].active = false 

将开关匹配到数组引用的一种简单方法是,为每个UISwitch在Storyboard中的一个标签匹配其在wordLists数组中的索引,将所有UISwitch挂接到相同的IBAction,然后在触发时使用:

 @IBAction func switchAction(_ sender: UISwitch) { wordList[sender.tag].active = rollIntoLoanSwitch.isOn } 

然后,您可以使用委托在每个切换或当您离开视图时重新发送信息。 另外,你可以在一个单独的类中定义你的单词列表,用单例表示并从任何地方引用它,这取决于你需要访问多less个不同的视图来访问单词列表。

您可以使用委托模式将消息从一个视图控制器发送到另一个。 对于委托模式,您将您的SecondViewController作为SettingsViewController的委托并设置两个类之间的通信。

如果必须将SettingsViewController中的数据传递给SecondViewController

在SettingsViewController中创build协议为

 protocol SettingsViewControllerDelegate { func settingDidFinished(data : [String]) } 

在SettingsViewController中创build一个id,以便您可以将任何类指定为其委托类。

 class SettingsViewController : UIViewController { // MARK:- Delegate var SettingsViewControllerDelegate ? 

在SettingsViewController的提交button或任何需要的地方调用协议方法。

  @IBAction private func doneTapped(_ sender: AnyObject) { delegate?.settingDidFinished(data : yourSettingsArray) } 

在SecondViewController中创buildSettingsViewController的对象,并将SecondViewController作为SettingsViewController的委托来分配

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if let settingsVc = segue.destinationViewController as? SettingsViewController { settingsVc .delegate = self } } 

在SecondViewController中实现SettingsViewController所需的协议方法

  extension SecondViewController: SettingsViewControllerDelegate { // # 4: "SecondViewController" implements "SettingsViewControllerDelegate " protocols: func settingDidFinished(data : [String]) { print("Settings finished") // use settings Array here. Access your settings here and add/substract array as per your requirements. } } 

希望它有助于..快乐编码.. 🙂