Swift - 即使我插入了不同的数字,文本字段也返回0

时间:2016-06-20 19:06:45

标签: ios swift text random

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

用户必须在两个不同的文本字段中写入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)之间的随机数)

1 个答案:

答案 0 :(得分:0)

确保您的网点(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)")
}