gpt4 book ai didi

swift - 如何从 UIAlertcontroller 获取文本输入或如何使用 Swift 等待输入

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

我正在尝试提供一个 Alertcontroller,提示用户输入文件名,然后在程序的其他地方使用该文件名。我一直在尝试以下代码的多种变体:

import UIKit

class ViewController: UIViewController {

var shortName: String!

@IBAction func saveFile(sender: AnyObject) {

//request filename with alert

var alertController:UIAlertController?
alertController = UIAlertController(title: "Enter File",
message: "Enter file name below",
preferredStyle: .Alert)

alertController!.addTextFieldWithConfigurationHandler(
{(textField: UITextField!) in
textField.placeholder = ""
})

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
self!.shortName = enteredText //trying to get text into shortName
print(self!.shortName) // prints
}
})

alertController?.addAction(action)
self.presentViewController(alertController!,
animated: true,
completion: nil)

//do some stuff with the input

print(shortName) //THIS RETURNS nil if uncommented. comment out to avoid crash

}


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.
}


}

我已经对此进行了彻底的研究,但也找不到如何:

  1. 从 UIAlertAction 闭包中获取 shortName 的字符串值并放入 shortName(我知道当前的“self!.shortName”在闭包外不可用 - 不管我使用什么名称 - 可以'不要把它弄出来)

  2. 如果您“按原样”运行程序,print(shortName) 行将由于 nil 的展开而导致崩溃。如何获得“等待”输入的警报?

大多数已发布的“解决方案”都有相同的问题 - 它们实际上并没有将文本输入从闭包中取出并放入一个可以被程序的其余部分访问的变量中。

谢谢

最佳答案

当然你会崩溃,shortName 是 nil 而没有按下提交按钮。你可以尝试这样的事情:

@IBAction func saveFile(sender: AnyObject) {

var alertController:UIAlertController?
alertController = UIAlertController(title: "Enter File",
message: "Enter file name below",
preferredStyle: .Alert)

alertController!.addTextFieldWithConfigurationHandler(
{(textField: UITextField!) in
textField.placeholder = ""
})

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
self!.shortName = enteredText //trying to get text into shortName
print(self!.shortName) // prints

self?.handleText()

NSOperationQueue.mainQueue().addOperationWithBlock({
self?.handleTextInMainThread()
})
}
})

alertController?.addAction(action)
self.presentViewController(alertController!,
animated: true,
completion: nil)
}

func handleText() {
print(self.shortName)
}

func handleTextInMainThread() {
print(self.shortName)
}

如果你想在用户输入后在 handleTextInMainThread 中使用 UI,你可以使用 NSOperationQueue

关于swift - 如何从 UIAlertcontroller 获取文本输入或如何使用 Swift 等待输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35067931/

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