这是一个简单的程序,可以创建一个随机数,并将其与用户在文本字段中输入的数字进行比较。它基本上是一个猜测我有多少手指?"程序
我想要做的是根据" 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.
}
}
答案 0 :(得分:0)
我在你的代码中做了一些清理工作。这未经过测试,但我想提供一些关于您未正确完成的事情的反馈:何时使用var
/ let
,正确的文件名是什么,请不要请使用;
等。请阅读评论。当你提供crashlog时我会修改这段代码 - 然后我会测试它。
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)
}