Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-打印出数字的当前值_Ios_Swift_Counter - Fatal编程技术网

Ios Swift-打印出数字的当前值

Ios Swift-打印出数字的当前值,ios,swift,counter,Ios,Swift,Counter,我目前正在开发一个计数器应用程序,每当你按下一个按钮,它就会计数。我已经这样做了,并且100%有效,我试图做的是再做一个按钮,当你按下它时,它会在控制台上显示当前的数字,但它只打印出0,因为这是我指定值的默认变量 我的全班同学: var counterNumber = 0 @IBOutlet weak var counterLabel: UILabel! func initCount(){ counterNumber = 0 } func numberUp(){ self.

我目前正在开发一个计数器应用程序,每当你按下一个按钮,它就会计数。我已经这样做了,并且100%有效,我试图做的是再做一个按钮,当你按下它时,它会在控制台上显示当前的数字,但它只打印出
0
,因为这是我指定值的默认变量

我的全班同学:

var counterNumber = 0

@IBOutlet weak var counterLabel: UILabel!

func initCount(){
    counterNumber = 0
}

func numberUp(){
    self.counterNumber++;
    counterLabel.text = "\(self.counterNumber)"
}

@IBAction func CountUp(sender: UIButton) {
    numberUp()
}

@IBAction func RestartButton(sender: UIButton) {
    initCount()
}


@IBAction func printButton(sender: UIButton) {
    self.numberUp();
    print(self.counterNumber)
}

override func viewDidLoad() {
    super.viewDidLoad()
    initCount()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

只需在
printButton:
操作中调用方法
numberrup

var counterNumber = 0//this is a variable of class
@IBOutlet weak var counterLabel: UILabel!

@IBAction func printButton(sender: UIButton) {
    self.numberUp();
    print(self.counterNumber)
}

func numberUp(){
        self.counterNumber++;
        counterLabel.text = "\(self.counterNumber)"
    }
使用“self”关键字调用实例变量


我已经这样做了,并且100%有效,那么问题是什么?“但是它只打印出0,因为这是我指定值的默认变量。”基本上我想打印出当前值。它只显示0,因为这是默认值。这意味着您没有在任何地方增加值(计数器)。你需要检查处理计数按钮操作的代码我已经更新了我的代码。代码很好,应该可以工作,现在只需要检查一件事,用正确的按钮检查IBActions绑定。删除所有iBaction绑定按钮并重新绑定相关操作。因为有可能重置和更新这两个操作绑定到同一个按钮上。仅打印“1”,因为默认值为0,并且
numberrup()
方法增加1如果下次单击,将输出什么?或者单击多次只是继续打印“1”仍然得到“1”你想看我的整个班级吗?用你的ViewController的完整代码编辑你的问题刚刚试过我得到
你按了==>可选(“共享结果”)按钮1次
你想让我在我的帖子编辑中发布我的整个班级代码吗?
class ViewController: UIViewController {

    // Properties
    var counterNumber:Int = 0    // Make this variable Global to Class

    // IBOutlets Properties
    @IBOutlet weak var counterLabel: UILabel!

    @IBAction func printButton(sender: UIButton) {
        self.numberUp()
        self.counterLabel.text = "\(self.counterNumber)"
        print("\n You Pressed ==> \(sender.title) button \(self.counterNumber) times")
    }

    func numberUp() {
        self.counterNumber += 1
        self.counterLabel.text = "\(counterNumber)"
    }
}