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

问题描述:

我正在尝试制作一个简单的flashcard应用程序。设置页面添加或减少不同的图像数组。用swift和xcode编码

概述 - 为了给你应用程序的基本运行,所有的flashcards都包含一个具有字母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

MY IDEA - 我想,我将不得不每连接接通和断开开关太其单词相应的数组,然后,如果编码了,则对于每个按钮的语句。然后将所有阵列添加到一起。但我不知道如何开始这样做。我制作了一个带有所有不同开关的表格视图,但还没有添加任何功能。我也不知道如何将信息从我的表格视图发送到第二个视图控制器。

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

这里有很多,但你似乎有一个很好的开始。

如果字样和类别相当固定的,那么我会做的第一件事就是建立分类词表为不同的类别,那么你可以使用组合你想要的那些为你的图像:

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情节板中的标签匹配其在生词数组索引,所有的UISwitches的钩相同的IBAction为当它被触发使用:

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

那么你可以使用一个Delega请在每个切换或当您离开视图时重新发送信息。或者,您可以在单独的类中定义您的单词列表,使用单例并从任何地方引用它,这取决于您需要访问单词列表的多少个不同视图。

+0

你好,我将你的建议分成不同的列表。但是我收到了一些错误。我在哪里把结构列表{让字:[字符串] var active:布尔}也如何声明imagelist.count而不是最大图像?我得到一个错误,并说有一个预期的声明。 –

+0

我只是有实施无功的ImageList麻烦:[字符串] { 让活跃= wordLists.reduce([]){(结果:[字符串]:清单)在 如果list.active { 返回结果+列表.words }其他{ 返回结果 }} 回报活跃 } –

+0

你能告诉我secondviewcontroller应该是什么样子?它不会让我在这里发布所有的代码,但是我认为我可能接近于破解这个,但有一些错误,我似乎无法修复 –

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

如果你不得不从SettingsViewController数据传递到SecondViewController

在SettingsViewController作为

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

创建协议创建SettingsViewController的ID,这样就可以指定任何类作为其委托类。在SettingsViewController

class SettingsViewController : UIViewController { 

// MARK:- Delegate 
    var SettingsViewControllerDelegate ? 

呼叫协议方法上提交按钮或其它地方需要。

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

希望它可以帮助..快乐编码.. :)

+0

我感谢你输入。你能推荐任何方式来区分不同的词组吗?我还没有实现一个成功的解决方案。 –