多个UIswitches一个随机化按钮

时间:2015-12-31 14:44:30

标签: ios swift uiswitch

我最近开始学习Swift,这是我第一次尝试学习如何编程,并开始了我的第一个应用程序。我使用了几个教程,到目前为止,我可以通过研究解决所有问题。现在我被卡住了。

我想创建一个应用程序,可以根据过滤器从阵列(鸡尾酒)中为我挑选一个随机鸡尾酒。为此,我创建了21个过滤器(例如cocktail.filter({!$o.orangeJuice})。这会使用橙汁从阵列中取出所有鸡尾酒。)。

创建UI我添加了21个UISwitch来切换是否必须应用过滤器。

我的随机按钮有效,并且显示了一个随机的鸡尾酒名称,但我无法让这些UISwitch工作。

请参阅我的代码:

var cocktailNoOrangeJuice = cocktails.filter({!$0.orangeJuice})
var cocktailNoLemonJuice = cocktails.filter({!$0.lemonJuice})
var cocktailNoAppleJuice = cocktails.filter({!$0.appleJuice})
var cocktailNoMaraJuice = cocktails.filter({!$0.maraJuice})
var cocktailNoLimeJuice = cocktails.filter({!$0.limeJuice})
var cocktailNoBananaJuice = cocktails.filter({!$0.bananaJuice})
var cocktailNoPeachJuice = cocktails.filter({!$0.peachJuice})
var cocktailNoCherryJuice = cocktails.filter({!$0.cherryJuice})
var cocktailNoJohanJuice = cocktails.filter({!$0.johanJuice})
var cocktailNoMangoJuice = cocktails.filter({!$0.mangoJuice})
var cocktailNoGrapefJuice = cocktails.filter({!$0.grapefJuice})
var cocktailNoTomatoJuice = cocktails.filter({!$0.tomatoJuice})
var cocktailNoCranbJuice = cocktails.filter({!$0.cranbJuice})
var cocktailNoBloodJuice = cocktails.filter({!$0.bloodJuice})
var cocktailNoPineapJuice = cocktails.filter({!$0.pineapJuice})
var cocktailNoCola = cocktails.filter({!$0.cola})
var cocktailNoSprite = cocktails.filter({!$0.sprite})
var cocktailNoBitter = cocktails.filter({!$0.bitter})
var cocktailNoTonic = cocktails.filter({!$0.tonic})
var cocktailNoGinger = cocktails.filter({!$0.ginger})
var cocktailNoAlc = cocktails.filter({!$0.noalc})

//this is a new array currently with the "noalc"-filter applied
var cocktailfiltered = cocktails.filter({!$0.noalc})


class ViewController: UIViewController {
  // this is one of the UISwitches 
@IBOutlet weak var lemon: UISwitch!


// The label for Cocktail output and the random button
@IBOutlet weak var ergebnis: UILabel!
@IBAction func random(sender: AnyObject) {

    let randomIndex = Int(arc4random_uniform(UInt32(cocktailfiltered.count)))

  ergebnis.text = (cocktailfiltered[randomIndex].name)
    }
}

如果这太傻了,请原谅我。我发现了如何获取UISwitch的状态(例如lemon.on ...)但不能使用此信息来应用过滤器。

非常感谢任何帮助。虽然我最初希望能够自己解决这个问题,但现在却让人感到沮丧。 请注意,鸡尾酒被定义为一个类别的成员,并且每种成分(例如橙汁)都会引发沸腾。因此,过滤器是手动工作的。但不是在UI中。

编辑:所以这是现在的版本。在我看来,感谢@vadian会让我的应用程序崩溃,这看起来要好得多。

类ViewController:UIViewController {

let keys = ["lemonJuice", "limeJuice", "bananaJuice", "pineapJuice", "maraJuice", "mangoJuice", "orangeJuice", "appleJuice", "peachJuice", "bloodJuice", "grapefJuice", "tomatoJuice", "cranbJuice", "cherryJuice", "johanJuice", "cola", "sprite", "bitter", "tonic", "ginger", "noalc"]
var states = [true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true]

@IBAction func changeState(sender: UISwitch) {
    let index = sender.tag
    states[index] = sender.on}

@IBOutlet weak var ergebnis: UILabel!
@IBAction func random(sender: AnyObject) {

    var conditions = [String]()
    for (index, state) in states.enumerate() {
        if state {
            conditions.append("(\(keys[index]) == TRUE)")
        }
    }
    let format = conditions.joinWithSeparator(" AND ")
    let predicate = NSPredicate(format:format)

    let filtered = (cocktails as NSArray).filteredArrayUsingPredicate(predicate)
    let randomIndex = Int(arc4random_uniform(UInt32(filtered.count)))

  ergebnis.text = (filtered[randomIndex].name)
    }

}

1 个答案:

答案 0 :(得分:2)

建议:

  1. 创建两个数组,一个用于自定义类中Bool属性的名称/键

    let keys = ["orangeJuice", "lemonJuice" ... ]
    

    和一个用于相应开关的状态

    var states = [false, false, ...]
    
  2. 根据密钥数组的顺序为UISwitch es分配标签。

  3. 创建更改状态IBAction并将操作分配给所有UISwitch es

    @IBAction func changeState(sender: UISwitch) {
      let index = sender.tag
      states[index] = sender.on
    }
    
  4. random函数中的
  5. 通过重复循环以编程方式创建NSPredicate,以获取true数组的所有states值以及keys的相应键{1}}数组。然后用该谓词过滤鸡尾酒阵列并获得随机鸡尾酒。

  6. PS:为了获得良好的用户体验,请在changeState功能中获取过滤后的鸡尾酒,并在没有鸡尾酒与所选成分匹配的情况下通知用户。

    更新

    创建谓词的示例

    var conditions = [String]()
    for (index, state) in states.enumerate() {
      if state {
         conditions.append("(\(keys[index]) == TRUE)")
      }
    }
    let format = conditions.joinWithSeparator(" AND ")
    let predicate = NSPredicate(format:format)