设置页面添加或减去不同的图像阵列。使用 swift 和 xcode 编码

settings page to add or subtract different image arrays. Coding with swift and xcode

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

概述 - 为了向您提供应用程序的基本 运行 介绍,所有抽认卡都包含一个带有字母 l 的对象。有时单词以字母 l 开头,有时有双 ll,有时单词以 pl 或 kl,或 sl 等开头...

到目前为止 - 我制作的应用程序有一个主页,从主页上按 Go,然后它也会带你第二个视图控制器。从那里你可以左右滑动浏览闪存卡。我通过创建一个包含所有图像的数组然后添加滑动手势(下面的代码)

来做到这一点
//
//  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.
}
*/

问题 - 我想创建一个设置页面。此设置页面将为每组单词提供一个基本的开关。所以假设我不想要任何首字母为 l 的单词,我也会将按钮关闭,然后当我滑动图像时,将不会有首字母为 l 的抽认卡。

settings page

我的想法 - 我想我必须将每个开关连接到相应的单词数组,然后编写 if,然后是每个按钮的语句。然后将所有数组加在一起。但是我完全不确定如何开始这样做。我制作了一个 Table 视图,其中包含所有不同的开关,但尚未添加任何功能。我也不确定如何将信息从 table 视图发送到第二个视图控制器。

我知道在一个问题中提出很多问题,但我们将不胜感激。谢谢

您可以使用委托模式将消息从一个视图控制器发送到另一个视图控制器。 对于委托模式,您将 SecondViewController 作为 SettingsViewController 的委托,并在两个 classes.

之间建立通信

如果您必须将数据从 SettingsViewController 传递到 SecondViewController

在 SettingsViewController 中创建协议为

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

在 SettingsViewController 中创建一个 id,这样您就可以将任何 class 指定为其委托 class.

class SettingsViewController : UIViewController {

// MARK:- Delegate
     var SettingsViewControllerDelegate ?

在提交按钮上或任何需要的地方调用 SettingsViewController 中的协议方法。

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

在 SecondViewController 中创建一个 SettingsViewController 对象,并将 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.
     }
 }

希望对您有所帮助.. 编码愉快..:)

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

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

let list1 = ["Bat", "Cow"]
let list2 = ["Frog", "Rabbit"]
let list3 = ["Parrot", "Tiger"]

var imageList: [String] {
  return list1+list2
}

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

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

但您最终可能需要一个比简单的字符串数组更易于管理的数据源来存储信息:

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

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

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

然后您可以使用委托在每次切换或离开视图时发回信息。或者,您可以在单独的 Class 中定义单词列表,使用单例并从任何地方引用它,这取决于您需要访问单词列表的不同视图的数量。