gpt4 book ai didi

Swift - 文本字段中的字符不可见

转载 作者:行者123 更新时间:2023-11-28 15:10:27 29 4
gpt4 key购买 nike

在我的应用程序中,我有几个定制设计的游戏来练习拼写和语法。在 iOS 11 发布之前,整个过程运行良好。

我的应用程序如何工作的概述:

  • 游戏从我们的服务器接收随机单词和图片,并自动创建一堆文本字段(在水平堆栈 View 中)
  • 文本字段的数量与接收到的单词的字母数量相似(例如,单词“throw”将创建五个彼此相邻的文本字段)
  • 该 View 链接到自定义键盘,该键盘会一直显示,直到所有问题都得到正确回答
  • 如果用户的输入与预期答案不相似,文本字段会自行重置

自 iOS 11 以来的问题

只有前两个问题不显示用户在各自文本字段中的输入(第一个字母似乎总是有效)。控制台还确认输入和字母比较确实有效,但文本字段始终为空。如果单词已正确回答,游戏甚至会完美重置并转到下一个问题。 奇怪的是第3、4、5题没有这个问题。

在下面的示例中,您可以看到发生了什么。第一个问题(“植物”)只显示第一个字母。第二个问题(“拍手”)做同样的事情。第三题(“咬”)没有这个问题。我的自定义光标工作正常并自动跳转到下一个框。

The first three questions

Swift 代码(仅功能)

let TEXT_FIELD_TAG = 1000
let CURSOR_INDEX = 0

var game: GameDictionary? {
didSet {

self.m_Client.GetMediaImage(game?.Image) { (image) in
self.imageView.image = image
}

self.selectedIndex = 0
self.answerChars = (game?.Content?.map { String($0).lowercased() })!
self.views = (0..<self.answerChars.count).map { _ in UITextField() }

var index: NSInteger = TEXT_FIELD_TAG

for textField in self.views {
textField.backgroundColor = .white
textField.textColor = Constants.MAIN_THEME_COLOR
textField.font = UIFont(name: Constants.GAME_FONT_TYPE_1, size: scaledFontSize(fontSize: 36))
textField.textAlignment = .center
textField.delegate = self
textField.layer.cornerRadius = 10
textField.autocapitalizationType = .none
textField.tag = index
textField.isUserInteractionEnabled = true

// Create the cursor object and animate the blinking
let cursor = UIView(frame: CGRect(x: scaledViewSize(viewSize: 10), y: scaledViewSize(viewSize: 10), width: 2, height: scaledViewSize(viewSize: 30)))
cursor.backgroundColor = Constants.MAIN_THEME_COLOR
cursor.isHidden = true

UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {() -> Void in
cursor.alpha = 0
}, completion: {(_ animated: Bool) -> Void in
cursor.alpha = 1
}
)

textField.addSubview(cursor)
self.container.addArrangedSubview(textField)

index+=1
}
}
}

// MARK: Question handlers
override func prepareForReuse() {
super.prepareForReuse()

inputArray.removeAll()
answerChars.removeAll()

self.selectedIndex = 0

for view in self.views {
view.removeFromSuperview()
}

self.views.removeAll()
}

override func onFocus() {
self.resetCursorTextField()

keyboardLauncher.onKeyPressed = { character in
self.views[self.selectedIndex].text = character
self.textChanged(sender: self.views[self.selectedIndex])
}

keyboardLauncher.showKeyboard()
keyboardLauncher.enableKeyboard()
}

func resetCursorTextField() {
for (index, textfield) in self.views.enumerated() {
textfield.subviews[CURSOR_INDEX].isHidden = (index == 0) ? false : true
}
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else { return true }
let newLength = text.count + string.count - range.length
return newLength <= 1
}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}

func resetInputViews() {
inputArray.removeAll()

self.selectedIndex = 0
self.resetCursorTextField()

for tag in TEXT_FIELD_TAG...(TEXT_FIELD_TAG + (self.answerChars.count) - 1) {
let textField = self.container.viewWithTag(tag) as! UITextField!
textField?.text = ""

if (tag == TEXT_FIELD_TAG) {
textField?.becomeFirstResponder()
}
}
}

func textChanged(sender: UITextField) {
if (sender.text?.count)! > 0 {
inputArray.append((sender.text?.lowercased())!)

if self.answerChars[self.selectedIndex] != inputArray.last! {
self.delegate?.updateAnswer(inputArray.joined(), false)
self.resetInputViews()

} else {
self.selectedIndex+=1

let nextField = sender.superview?.viewWithTag(sender.tag + 1) as! UITextField!
nextField?.becomeFirstResponder()

sender.subviews[CURSOR_INDEX].isHidden = true
nextField?.subviews[CURSOR_INDEX].isHidden = false

if self.answerChars.count == inputArray.count {
playCorrectAnswer()
isCompleted = true

keyboardLauncher.disableKeyboard()

self.textField?.isEnabled = false
self.delegate?.updateAnswer(inputArray.joined(), true)
self.delegate?.nextQuestion(self)
}
}
}
}

CellForItemAtIndexpath

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == self.m_Collection?.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: gameScoreOverviewCell, for: indexPath) as! GameScoreOverviewCell
cell.closeViewButton.addTarget(self, action: #selector(closeView(sender:)), for: .touchUpInside)
cell.rulesButton.addTarget(self, action: #selector(handleAlertScoreOverview(sender:)), for: .touchUpInside)

return cell
}

let game = self.m_Collection![indexPath.item]

if game.StarryGrade == StarryGrade.Grade4 && game.Level == Level.Level1 {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: fourthGradeCellLevel1, for: indexPath) as! GameCellFourthGradeLevel1
cell.cancelButton.addTarget(self, action: #selector(closeViewWithAlert(sender:)), for: .touchUpInside)
cell.questionLabel.text = "Question \(indexPath.item + 1) of \(self.m_Collection!.count)"
cell.game = game
cell.delegate = self

keyBoardIsVisible = true

return cell
}
}

最佳答案

这可能不是程序问题,而是接口(interface)问题。尝试以下操作:

  1. 确保您正在为调试编译应用

  2. 重现下左图中的场景,您在其中键入了一些字母,但它们被隐藏了: enter image description here

  3. 在调试导航器中单击此按钮:

enter image description here

  1. 选择查看 UI 层次结构
  2. 在 Xcode 中解构您的 UI,单击包含隐藏字符的文本字段。
  3. 您现在应该检查几件事情:

    • 在右侧面板的对象检查器中,检查以确保文本字段的 Title 属性是您键入的字符(如果不是,则文本字段未捕获键盘输入)
    • 同样在 Object Inspector 中,确保 View Visibility 属性设置为 Not Hidden
    • 在大小检查器(也在右侧面板中)中,检查以确保文本字段足够大以显示字符

我以前遇到过类似的问题,这些步骤让我能够确定问题的原因。我希望这会有所帮助。

关于Swift - 文本字段中的字符不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47765684/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com