简体   繁体   English

如何根据变量在SWIFT中更改图像,单击使用IF语句的按钮

[英]How to change an image in SWIFT depending on a variable, clicking a button that uses an IF statement

This is a simple program that creates a random number and compares it to a number that the user inputs in a text field. 这是一个简单的程序,它创建一个随机数并将其与用户在文本字段中输入的数字进行比较。 It's basically a "guess how many fingers i'm holding up?" 这基本上是一个“猜测我举了几根手指?” program. 程序。

What I want to do is to display an image in the UIImageView depending on the "var randomNumber" when the user guessed correctly, and an image (cross) for when the user guesses incorrectly or has input a number higher than 5. The image shows a question mark at the beginning. 我想要做的是在用户正确猜测时根据“ var randomNumber”在UIImageView中显示图像,以及在用户错误猜测或输入的数字大于5时显示图像(十字)。一开始是一个问号。 The images from 0-5 are images showing 5 fingers to none accordingly. 从0-5的图像是相应地显示5根手指的图像。

Do I have to specify the images like this? 我必须指定这样的图像吗? Do i need to write down the extension of the file? 我需要写下文件的扩展名吗?

hand.image = UIImage(named: "0")
hand.image = UIImage(named: "1")
hand.image = UIImage(named: "2")
hand.image = UIImage(named: "3")
hand.image = UIImage(named: "4")
hand.image = UIImage(named: "5")
hand.image = UIImage(named: "wrong")
hand.image = UIImage(named: "?")

or like this? 还是这样?

let image0 = UIImage(named: "0")
let image1 = UIImage(named: "1")
let image2 = UIImage(named: "2")
let image0 = UIImage(named: "3")
let image1 = UIImage(named: "4")
let image2 = UIImage(named: "5")
let image0 = UIImage(named: "wrong")
let image1 = UIImage(named: "?")

Do I need to specify them at all? 我是否需要指定它们?

Nothing has worked so far, would be nice if someone could help. 到目前为止,没有任何工作,如果有人可以帮助,那就太好了。 The app crashes every time I press the "Guess Button". 每次我按“猜测按钮”时,应用程序崩溃。 It's a fairly easy task, but as i'm just starting out i would like to know how it's done. 这是一个相当容易的任务,但是当我刚开始时,我想知道它是如何完成的。 Thanks in advance. 提前致谢。

This is my code so far. 到目前为止,这是我的代码。

//
//  ViewController.swift
//  Finger Raten
//
//  Created by Daniel Bleyer on 04.07.15.
//  Copyright (c) 2015 DiBi. All rights reserved.
//

import UIKit

class ViewController: UIViewController{

@IBOutlet var hand: UIImageView!                   //image view

@IBOutlet weak var output: UILabel!                 //result (right/wrong)

@IBOutlet weak var input: UITextField!              //guessed number

@IBAction func guess(sender: AnyObject)        //guess button (comparing both numbers)

{


    var randomNumber = arc4random_uniform(6)  //random number

    var inputInt = input.text.toInt()                            //guessed number

    hand.image = UIImage(named: "0")          //does this code go here? or somewhere else?
    hand.image = UIImage(named: "1")          //does this code go here? or somewhere else?
    hand.image = UIImage(named: "2")          //does this code go here? or somewhere else?
    hand.image = UIImage(named: "3")          //does this code go here? or somewhere else?
    hand.image = UIImage(named: "4")          //does this code go here? or somewhere else?
    hand.image = UIImage(named: "5")          //does this code go here? or somewhere else?
    hand.image = UIImage(named: "wrong")  //does this code go here? or somewhere else?
    hand.image = UIImage(named: "?")          //does this code go here? is it RIGHT? This image
                                                                      //shows at the beginning, it is a question mark.


    if inputInt != nil && inputInt < 6                                                     //conditions (not empty/0-5)

    {

        if inputInt == Int(randomNumber)                                             //comparing random/guessed

        {

        output.text = "Right !";                                                              //right guess
        hand.image = UIImage(named: "\(randomNumber)")              //image showing 0-5 fingers
                                                                                                         //is this the right place?
            input.resignFirstResponder();                                              //hides numpad

        } else  {

                output.text = "Wrong, it was a \(randomNumber)";          //wrong guess, it was a ??
                hand.image = UIImage(named: "wrong)")                       //image of a cross (X)
                                                                                                         //is this the right place?
                    input.resignFirstResponder();                                      //hides numpad
                }


    } else  {

            output.text = "Enter a number from 0-5";                             //field empty or out of range
            hand.image = UIImage(named: "wrong")                            //image of a cross (X)
                                                                                                         //is this the right place?
                input.resignFirstResponder();                                          //hides numpad
            }
}

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.
    }
}

I've made a little bit of cleanup in your code. 我在您的代码中做了一些清理。 This is not tested, but I wanted to provide feedback about few things you're doing not completely correctly: when to use var / let , what are the proper file names, don't use ; 这未经测试,但是我想提供一些您未完全正确完成的事情的反馈:何时使用var / let ,什么是正确的文件名,不要使用; etc. Please read comment. 等等。请阅读评论。 I will revise this code when you provide crashlog - and I'll test it then. 当您提供崩溃日志时,我将修改此代码-然后我将对其进行测试。

let maxInputInt: Int = 5 // this way you later don't search through the code to change value

override func viewDidLoad() {
    super.viewDidLoad()

    self.reset()
}

func reset() {
    output.text = NSLocalizedString("Enter a number from 0-5", ""); // always provide strings as NSLocalizedString - app is ready to translate in future versions. Without it you'd have to search through all source files
    // BTW you should name variables such way so it's obvious what is it, eg inputTextField, output/input is rather wrong
    hand.image = UIImage(named: "?")
}

func guess(sender: AnyObject) {
    var randomNumber = arc4random_uniform(maxInputInt + 1)

    let inputInt = input.text.toInt() // this won't change, should be declared as let not int
    let wrongImage = UIImage(named: "wrong")

    if inputInt <= maxInputInt { // int is not an object, it cannot be nil, nil is for objects only
        if inputInt == Int(randomNumber) {
            output.text = NSLocalizedString("Right !", "");
            hand.image = UIImage(named: "\(randomNumber)")
        } else {
            output.text = NSLocalizedString("Wrong, it was a \(randomNumber)", "")
            hand.image = wrongImage
        }
    } else {
        hand.image = wrongImage
    }
    input.resignFirstResponder() // you do it under all conditions, so don't repeat yourself (DRY)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM