为按钮赋予价值-Swift [英] Give Values to Buttons - Swift

查看:44
本文介绍了为按钮赋予价值-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我.现在,当我单击按钮0时,它将自动将我带到第二个屏幕以进行测试.现在,我想相应地给数字赋值,当单击的数字等于标签上的数字时,它将显示第二个屏幕.如果不是,问号标签将闪烁或更改颜色.谢谢

I was wondering if someone can help me with this. Right now when I click on the button 0 it will automatically take me to the 2nd screen for testing purposes. Now I want to give value to the numbers accordingly and when the number clicked is equals to the number on my label than it will show the 2nd screen. If not, the Question mark label will blink or change color. Thanks

推荐答案

将此变量放在viewDidLoad()上方:

Place this variable above viewDidLoad():

var chosenNumber: Int?

将您的按钮连接到这样的IBAction上,然后在情节提要中使它们的每个标签都等于其面值.

Attach your buttons to an IBAction like this one and in the storyboard make each of their tags equal to their face value.

@IBAction func aNumberButtonWasPressed(_ sender: UIButton) {
    chosenNumber = sender.tag
    performSegue(withIdentifier: "theSegueIdentifier", sender: nil)
}

像这样将数字传递给以下视图控制器,然后使用该数字相应地填充标签.

Pass the number to the following view controller like so and then use that number to populate the label accordingly.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    if let vc = segue.destination as? TheViewControllerYouAreGoingTo {
        vc.theVariableYouWantToSaveYourNumberTo
    }
}

如果您想知道如何做一些不同的事情,请问.

If you want to know how to do something differently, just ask.

如果您想真正地学习敏捷,我强烈建议您免费参加此Paul Hegarty课程. Paul Hegarty课程

If you want to really learn swift well, I highly recommend taking this Paul Hegarty Course for FREE. Paul Hegarty Course

前两节课将专门教您有关适当的快速语法和尽早构建计算器的所有知识.这似乎有些先进,但这就是为什么我现在看了3次了.自从我开始学习Swift之后,每隔3-6个月就会帮助我提高技能.

The first two lessons specifically will teach you everything you could want to know about proper swift syntax and building a calculator early on. It might seem slightly advanced, but that is why I have watched the course 3 times now. Every 3-6 months since I started learning Swift this has helped refine my skills.

let thisString = label.text
let thisInt = Int(thisString)

thisInt将是可选的,因此在某些时候,您需要将其解包.您可以根据需要使用它,并将其设置为另一个标签,如下所示:

thisInt will be an optional, so at some point you will need to unwrap it. You can use it however you want to and set it to another label like so:

anotherLabel.text = "\(thisInt)"

这篇关于为按钮赋予价值-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆