gpt4 book ai didi

ios - SWIFT代码;无法使用标准方法关闭键盘

转载 作者:行者123 更新时间:2023-11-29 02:37:50 24 4
gpt4 key购买 nike

我一直在编写 Swift 教程。在教程中,作者忽略了解释如何关闭模拟器上的键盘。我尝试了我所知道的标准方法; resignFirstResponder()但是我不断收到错误消息;不能将字符串转换为 bool 值。到目前为止,我还没有找到关于 Stack overflow 的可行答案。有任何想法吗?我在下面包含了代码。

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {


@IBOutlet weak var yearTextField: UITextField!

@IBOutlet weak var dayTextField: UITextField!

@IBOutlet weak var monthTextField: UITextField!


@IBOutlet weak var messageLabel: UILabel!



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

self.yearTextField.delegate = self
self.dayTextField.delegate = self
self.monthTextField.delegate = self
}

// Suggested fix not working?

// func textFieldShouldReturn(textField: UITextField!) -> Bool {
// textField.resignFirstResponder()
// return true;
// }


@IBAction func daysButtonPressed(sender: AnyObject) {

println("Days Button Pressed")

messageLabel.text = "Button pressed"

messageLabel.resignFirstResponder()



var dateComponents = NSDateComponents()
dateComponents.day = dayTextField.text.toInt()! // 28
dateComponents.month = monthTextField.text.toInt()! // 3
dateComponents.year = yearTextField.text.toInt()! // 1956

var calendar = NSCalendar(identifier: NSGregorianCalendar)
var birthDate = calendar.dateFromComponents(dateComponents) //NSDate
var currentDate = NSDate.date()


println(birthDate)
println("Current Date \(currentDate)")


var durationDateComponets = calendar.components(NSCalendarUnit.CalendarUnitDay,
fromDate: birthDate!,
toDate: currentDate,
options: nil)


var numberOfDaysAlive = durationDateComponets.day

//Format with commas

var numberFormatter = NSNumberFormatter()
numberFormatter.usesGroupingSeparator = true

var dayString = numberFormatter.stringFromNumber(numberOfDaysAlive)



messageLabel.text = "Days alive: \(dayString)"


// self.messageLabel.resignFirstResponder()


}


}

最佳答案

键盘已打开,因为您的一个 UITextField 是第一响应者。由于您不知道是哪一个,因此请对每个调用 resignFirstResponder()

daysButtonPressed()中而不是调用:

messageLabel.resignFirstResponder()

您需要调用:

dayTextField.resignFirstResponder()
monthTextField.resignFirstResponder()
yearTextField.resignFirstResponder()

关于ios - SWIFT代码;无法使用标准方法关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26150695/

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