【问题标题】:Swift - the text field returns 0 even though I inserted a different numberSwift - 即使我插入了不同的数字,文本字段也会返回 0
【发布时间】:2016-06-20 19:06:44
【问题描述】:

我是 Swift 新手,我正在尝试制作一个小应用程序,给定两个值 (a,b) 返回两个数字之间的随机数 (a

用户必须在两个不同的文本字段中写入 a 和 b 的值,然后只需按下一个按钮就可以了

问题是: 我告诉 NSLog 在生成随机数之前显示这两个值,因为我注意到某些东西没有按预期工作,结果显然我插入的第二个值,无论是 a 还是 b,对于某些人(未知)原因,等于 0 !例如,如果我在 a-textfield 中写入值 10,然后在 b-textfield 中插入值 40,则 NSLog 显示 a = 10, b = 0。如果我之前插入 b 值,则 NSLog 显示 b = 40,a = 0。 这些是代码行:

@IBAction func TextAAction(sender: AnyObject) {
    a = UInt32(TextA.text!)!

}


@IBAction func TextBAction(sender: AnyObject) {
    b = UInt32(TextB.text!)!

}


@IBAction func randomInBetweenAction(sender: AnyObject) {
    NSLog("a=%ld, b=%ld", a, b)
    randomInBetween()
    unHide()
    Label.text = "\(randomNumberInBetween)"

}

如您所见,值 a 等于插入到 TextA 字段中的值,值 b 等于插入到 TextB 字段中的值

randomInBetween() 是在 a 和 b 之间生成随机数的函数,它确实有效。问题是其中一个值(我插入的第二个)出于某种原因始终为 0,即使我将它设置为不同的数字!

关于为什么会发生这种情况以及如何解决的任何想法?

@ReinerMelian 这是完整代码

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var Label: UILabel!
@IBOutlet weak var Button: UIButton!
@IBOutlet weak var InvariableLabel: UILabel!
@IBOutlet weak var TextA: UITextField!
@IBOutlet weak var TextB: UITextField!
@IBOutlet weak var AndLabel: UILabel!
@IBOutlet weak var Button2: UIButton!

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

    Hide()

}

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

var randomNumber = Int()


func randomNUMBER() -> Int {
    randomNumber = Int(arc4random())
    return randomNumber
}

func Hide() {
    Label.hidden = true
}

func unHide() {
    Label.hidden = false
}

var a = UInt32()
var b = UInt32()

var randomNumberInBetween = Int()

func randomInBetween() -> Int {
    if b>a {
        randomNumberInBetween = Int(arc4random_uniform(b - a) + a)
         return randomNumberInBetween

    }  else {
randomNumberInBetween = Int(arc4random_uniform(a - b) + a)
return randomNumberInBetween
}


}



@IBAction func ButtonAction(sender: AnyObject) {
    randomNUMBER()
    unHide()
    Label.text = "\(randomNumber)"
    NSLog("\(randomNumber)")
}


@IBAction func TextAAction(sender: AnyObject) {
    a = UInt32(TextA.text!)!


}


@IBAction func TextBAction(sender: AnyObject) {
    b = UInt32(TextB.text!)!

}


@IBAction func randomInBetweenAction(sender: AnyObject) {
    NSLog("a=%ld, b=%ld", a, b)
    randomInBetween()
    NSLog("\(randomInBetween)")
    unHide()
    Label.text = "\(randomNumberInBetween)"
    NSLog("\(randomInBetween)")
}



}

(PS:正如你所看到的,还有一个函数可以生成一个没有任何约束的随机数,但那部分工作得很好..问题出在代码的另一部分,即返回的部分两个给定值之间的随机数 (a,b) )

【问题讨论】:

  • 您的 ab 变量也是 UInt32 吗?
  • 此代码@IBAction func excuteCode(sender: AnyObject) { let a : Int32 = Int32(textA.text!)! let b : Int32 = Int32(textB.text!)! NSLog("a=%ld, b=%ld", a, b) }对我有效
  • @ReinierMelian a 和 b 都是 UInt32,是的。我不知道我的代码有什么问题,但是 a 或 b 总是等于 0.. 这让我发疯了!
  • 你能发布你的其他代码吗?看看发生了什么?因为我在上一条评论中发布的代码运行良好
  • 触发TextAActionTextBAction的实际操作是什么?点击按钮?

标签: ios swift text random


【解决方案1】:

确保您的插座(TextATextB)和您的 IBAction(randomInBetweenAction)连接正确,然后移除

@IBAction func TextAAction(sender: AnyObject) {
    a = UInt32(TextA.text!)!

}

@IBAction func TextBAction(sender: AnyObject) {
    b = UInt32(TextB.text!)!

}

点击按钮试试这个功能:

@IBAction func randomInBetweenAction(sender: AnyObject) {
    a = UInt32(TextA.text!)!
    b = UInt32(TextB.text!)!
    NSLog("a=%ld, b=%ld", a, b)
    randomInBetween()
    NSLog("\(randomInBetween)")
    unHide()
    Label.text = "\(randomNumberInBetween)"
    NSLog("\(randomInBetween)")
}

【讨论】:

  • 非常感谢,它成功了!我不知道为什么,但出于某种原因,我真的认为我必须对每个文本字段进行操作,以便告诉他们每个人在用户插入值时要做什么......但这只是没有必要!谢谢:)
  • 好吧。只要用户更改文本,您就可以添加一个操作以将文本字段的 text 分配给您的变量 a / b ......但在您的情况下,当用户按下按钮生成随机数。很高兴我能帮忙:)
猜你喜欢
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
相关资源
最近更新 更多