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

时间:2017-07-04 02:20:48

标签: ios swift xcode uitableview uiswitch

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

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

SO FAR - 我已经制作了应用程序,以便有一个主页,从主页上按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

我的想法 - 我想我将必须连接每个on和off开关太相应的单词数组然后编码如果,然后每个按钮的语句。然后将所有数组添加到一起。但是我根本不确定如何开始这样做。我已经使用所有不同的on和off开关制作了一个Table视图,但还没有添加任何功能。我也不确定如何将表格视图中的信息发送到第二个视图控制器。

我知道在一个问题中有很多问题,但是我们非常感谢任何帮助。谢谢

2 个答案:

答案 0 :(得分:0)

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

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

在SettingsViewController中创建协议

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

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

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.
     }
 }

希望它有所帮助..快乐编码.. :)

答案 1 :(得分:0)

这里有很多,但你似乎已经有了一个良好的开端。

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

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

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

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然后在触发时使用:< / p>

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

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