gpt4 book ai didi

ios - 在警报中点击 Return Key Tap 时的操作

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:09 26 4
gpt4 key购买 nike

如何捕获返回键按下并在警报内对文本字段执行操作?这是警报的代码:

    var alert = UIAlertController(title: "Add Country", message: "Add a country to the Speaker's List", preferredStyle: .Alert)

let saveAction = UIAlertAction(title: "Save", style: .Default) {
(action: UIAlertAction!) -> Void in
let textField = alert.textFields![0] as! UITextField
self.countries.append(textField.text)
self.speakersListTableView.reloadData()
}

let cancelAction = UIAlertAction(title: "Cancel", style: .Default) {
(action: UIAlertAction!) -> Void in
}

alert.addTextFieldWithConfigurationHandler {
(textField: UITextField!) -> Void in
}

alert.addAction(saveAction)
alert.addAction(cancelAction)

最佳答案

您需要设置文本字段的委托(delegate)并在您的类中实现 UITextFieldDelegate。像这样:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate
{
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.
}

override func viewDidAppear(animated: Bool)
{
var alert = UIAlertController(title: "Add Country", message: "Add a country to the Speaker's List", preferredStyle: .Alert)

let saveAction = UIAlertAction(title: "Save", style: .Default) {
(action: UIAlertAction!) -> Void in
let textField = alert.textFields![0] as! UITextField
//self.countries.append(textField.text)
//self.speakersListTableView.reloadData()
}

let cancelAction = UIAlertAction(title: "Cancel", style: .Default) {
(action: UIAlertAction!) -> Void in
}

alert.addTextFieldWithConfigurationHandler {
(textField: UITextField!) -> Void in
textField.delegate = self
}

alert.addAction(saveAction)
alert.addAction(cancelAction)

self.presentViewController(alert, animated: true) { () -> Void in
}
}


func textFieldDidBeginEditing(textField: UITextField)
{
println("textFieldDidBeginEditing")
}

func textFieldDidEndEditing(textField: UITextField) {
println("textFieldDidEndEditing")
}

}

关于ios - 在警报中点击 Return Key Tap 时的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30224110/

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