我正在制作基于文本的iOS游戏,该游戏使用由UITextView(可滚动,不可编辑)和UITextField组成的单个视图。当我在编辑textField时按回车键时,键盘关闭,textField的文本被添加到textView中。但是,UITextView的文本根本没有出现。无论我如何尝试显示它,背景颜色仍然会出现,但没有其他颜色。我已经尝试过设置字体大小和颜色。
这是ViewController中处理两个UI组件的代码。
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
// outlets / actions
@IBOutlet weak var outputView: UITextView!
@IBOutlet weak var inputField: UITextField!
var uni: Universe? // a custom class that contains the game's data
override func viewDidLoad() {
//outputView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)
inputField.delegate = self
outputView.delegate = self
inputField.text = "press return for help"
outputView.text = ""
outputView.textColor = UIColor.greenColor()
outputView.frame = self.view.frame
// The next 3 lines make a new Universe object.
uni = Assembler.build(outputView)
uni!.setUni()
uni!.setParser()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
uni!.player.viewLocation()
}
// delegate method; this should do 3 things:
// 1. remove the keyboard
// 2. call uni.parser.read(text)
// 3. remove the text in the field
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
outputView.text! += textField.text! + "\n"
uni!.parser!.read(textField.text!) // This function interprets the text from textField and prints something appropriate in the textView.
textField.text = ""
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// The rest of the code makes the whole frame shift up/down when the keyboard appears/disappears to keep the UITextField visible.
}
任何帮助都将非常感谢。这个网站上的类似问题没有给我任何东西。