将开关状态绑定到Swift

时间:2017-02-08 19:36:41

标签: ios swift

我正在通过构建一个基本的计数应用程序(基于本教程https://www.youtube.com/watch?v=3blma4PCRak)快速学习,并且我正在尝试根据交换机的状态设置一个以3为增量的函数。

我在网上和SO中的教程中找到的答案似乎很多年前很多,并且很多语法都被弃用了,包括:

•而不是oddSwitch.On现在是oddSwitch.isOn

•说明颜色从UIColor.redColor更改为UIColor.red

依此类推......以下是我的代码:

//
//  ViewController.swift
//  TestApp
//
//  Created by kawnah on 2/8/17.
//  Copyright © 2017 kawnah. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet var outputLabel: UILabel? = UILabel();

    var currentCount = 0;


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func addOneBUtton(_ sender: UIButton) {

        currentCount = currentCount + 1

        if currentCount <= 1 {
            outputLabel?.text = "\(currentCount) click"
        } else {
            outputLabel?.text = "\(currentCount) clicks"
        }

        outputLabel?.textColor = UIColor.red

    }


    @IBOutlet var oddSwitch: UISwitch!

    @IBAction func oddToggle(_ sender: UISwitch) {
        if oddSwitch!.isOn {
            currentCount = currentCount + 3
        }
    }

}

我对@IBOutlet和我的函数之间的关系感到困惑,该函数表示以三为增量进行计数。我也试过包含弱存储,但据我所知,这是@IBOutlet的默认值。目前,当我打开开关时,计数器仍然增加1.

我的错误日志只显示我的启动画面的PNG压缩错误,而不是代码。究竟发生了什么?

1 个答案:

答案 0 :(得分:1)

更改addOneBUtton代码以匹配此内容。

    Dim rngData As Range
    Dim LastRow As Long
    Dim LastCol As Long
    Dim pvtItm As PivotItem
    Dim ListFields(0 To 5) As String
    Dim PivotTable_Name(0 To 3) As String
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer

    **For k = 0 To 3
        For n = 0 To 3
            PivotTable_Name(n) = "PivotTable" & "n"
        Next n

        wsPvtTbl.PivotTables.Add PivotCache:=PvtTblCache, TableDestination:=wsPvtTbl.Cells(3, 1 + 4 * k), TableName:=PivotTable_Name(k), DefaultVersion:=xlPivotTableVersion11
        Set PvtTbl = wsPvtTbl.PivotTables(PivotTable_Name(k))**

        PvtTbl.ManualUpdate = True

        For j = 0 To LastCol - 1
            ListFields(j) = wsData.Cells(1, j + 1)
        Next j

        'add row, column and page (report filter) fields:
        Set pvtFld = PvtTbl.PivotFields(ListFields(1))
        pvtFld.Orientation = xlPageField

        Set pvtFld = PvtTbl.PivotFields(ListFields(2))
        pvtFld.Orientation = xlRowField

        For i = 3 To LastCol - 1
            With PvtTbl.PivotFields(ListFields(i))
                .Orientation = xlDataField
                .Function = xlSum
                .NumberFormat = "#,##0"
                .Position = i - 2
            End With
        Next i                
        PvtTbl.ManualUpdate = False            
    Next k

End Sub

您可以删除此部分。

@IBAction func addOneBUtton(_ sender: UIButton) {

    if oddSwitch!.isOn {
        currentCount = currentCount + 3
    } else {
        currentCount = currentCount + 1
    }
    if currentCount <= 1 {
        outputLabel?.text = "\(currentCount) click"
    } else {
        outputLabel?.text = "\(currentCount) clicks"
    }

    outputLabel?.textColor = UIColor.red

}

所以完整的代码看起来应该是这样的。

@IBAction func oddToggle(_ sender: UISwitch) {
    if oddSwitch!.isOn {
        currentCount = currentCount + 3
    }
}

注意:这是我在使用import UIKit class testViewController: UIViewController { //MARK: IBOutlets @IBOutlet var outputLabel: UILabel? = UILabel(); @IBOutlet var oddSwitch: UISwitch! //MARK: Vars var currentCount = 0; //MARK: Lifecyle override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } //MARK: IBActions @IBAction func addOneBUtton(_ sender: UIButton) { if oddSwitch!.isOn { currentCount = currentCount + 3 } else { currentCount = currentCount + 1 } if currentCount <= 1 { outputLabel?.text = "\(currentCount) click" } else { outputLabel?.text = "\(currentCount) clicks" } outputLabel?.textColor = UIColor.red } } 所有内容保持代码清洁并且使用IBOutlets更快地跳转到代码部分时的转折。