【问题标题】:How to change an image in SWIFT depending on a variable, clicking a button that uses an IF statement如何根据变量更改 SWIFT 中的图像,单击使用 IF 语句的按钮
【发布时间】:2015-09-22 13:40:27
【问题描述】:

这是一个简单的程序,它创建一个随机数并将其与用户在文本字段中输入的数字进行比较。这基本上是一个“猜猜我举起多少根手指?”程序。

我想要做的是在用户猜测正确时根据“var randomNumber”在 UIImageView 中显示图像,并在用户猜测错误或输入大于 5 的数字时显示图像(交叉)。图像在开头显示一个问号。从 0 到 5 的图像是相应地显示 5 根手指的图像。

我必须像这样指定图像吗?我需要写下文件的扩展名吗?

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: "?")

还是这样?

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: "?")

我是否需要指定它们?

到目前为止没有任何效果,如果有人可以提供帮助,那就太好了。每次我按下“猜测按钮”时,应用程序都会崩溃。这是一项相当容易的任务,但由于我刚刚开始,我想知道它是如何完成的。提前致谢。

这是我目前的代码。

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

【问题讨论】:

  • hand.image 的多重赋值应该被删除,它们什么都不做。您的项目中包含的图像的实际名称是什么?使用该名称(忽略任何 @2x 或 3x 等)
  • 图像被称为 (0.png, 1.png ... 5.png, ?.png, wrong,png)。它们与 ViewController.swift 和 Main.storyboard 位于同一文件夹中。
  • 使用UIImage(named: "\(randomNumber).png") 不起作用???
  • 我称它们为 0.png... 5.png 以便图像会根据创建的随机数而变化。
  • 无论有没有扩展,它都会崩溃。

标签: ios image swift button click


【解决方案1】:

我在您的代码中做了一些清理工作。这未经测试,但我想提供一些关于您没有完全正确做的事情的反馈:何时使用var/let,什么是正确的文件名,不要使用; 等。请阅读评论。当您提供崩溃日志时,我将修改此代码 - 然后我将对其进行测试。

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

【讨论】:

  • 感谢您的提示,我将开始更好地命名事物并且不会使用“;”。我无法建造......目前你所做的方式对我来说太先进了。还是谢谢!
  • 如果这太高级了,那么你应该从阅读一些文档开始——也许在 RayWenderlich 页面上查看一些教程——有很多有用的提示。正如我所说,提供的代码未经测试,在您提供崩溃日志之前我不会对其进行测试,因为我不想完全改变您的概念和实现。
猜你喜欢
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
相关资源
最近更新 更多