gpt4 book ai didi

ios swift 2 : extension - Alert with textfield

转载 作者:行者123 更新时间:2023-11-30 13:06:47 24 4
gpt4 key购买 nike

我正在尝试使用 uialertcontroller 和使用扩展 uialertcontroller 的文本字段创建一个函数

这是我的代码:

extension UIAlertController{

class func AlertWithTextField(here: String, message1 : String) -> UIAlertController{

var alertController:UIAlertController?
alertController = UIAlertController(title: here,
message: message1,
preferredStyle: .Alert)

alertController!.addTextFieldWithConfigurationHandler(
{(textField: UITextField!) in
textField.placeholder = "Ex: 1"
textField.textAlignment = .Center
textField.delegate = self
textField.keyboardType = UIKeyboardType.NumberPad
})
let action = UIAlertAction(title: "Submit",
style: UIAlertActionStyle.Default,
handler: {[weak self]
(paramAction:UIAlertAction!) in
if let textFields = alertController?.textFields{
let theTextFields = textFields as! [UITextField]
let enteredText = theTextFields[0].text
print("\n\(enteredText)") }
})
let action2 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alertController?.addAction(action)
alertController?.addAction(action2)

}}

好吧,我对“self”这个词有疑问,我找不到它们的解决方案,这个问题的解决方案是什么??

最佳答案

对于你的第一个 self 问题,我建议你做这样的事情

    class func AlertWithTextField(here: String, message1 : String, delegate:UITextFieldDelegate?) -> UIAlertController{

var alertController:UIAlertController?
alertController = UIAlertController(title: here,
message: message1,
preferredStyle: .Alert)

alertController!.addTextFieldWithConfigurationHandler(
{(textField: UITextField!) in
textField.placeholder = "Ex: 1"
textField.textAlignment = .Center
textField.delegate = delegate
textField.keyboardType = UIKeyboardType.NumberPad
})
let action = UIAlertAction(title: "Submit",
style: UIAlertActionStyle.Default,
handler: {(paramAction:UIAlertAction!)->Void in
if let textFields = alertController?.textFields {
let theTextFields = textFields
let enteredText = theTextFields[0].text
print("\n\(enteredText)") }
})
let action2 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alertController?.addAction(action)
alertController?.addAction(action2)
return alertController!
}

您可以在静态方法中接受 UITextFieldDelegate 对象并将其分配给委托(delegate),对于第二个问题,您声明了弱 self 但不在闭包中使用它,因此只需删除它,您的代码应该可以正常工作。

关于ios swift 2 : extension - Alert with textfield,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39268331/

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