gpt4 book ai didi

ios - 使用展开转场从文本字段传递数据时出现问题

转载 作者:行者123 更新时间:2023-11-30 12:46:10 26 4
gpt4 key购买 nike

我正在遵循 this 的已接受答案问题,但我在将数据从 ViewControllerB (第二个 View Controller )传递回 ViewController (初始 View Controller )时遇到问题。根据我添加的一些打印语句的输出,我认为 ViewController 的展开发生在 textFieldDidEndEditing 之前。

dataPassed from ViewControllerB: Default passed data
newValue: Default passed data
textFieldDidEndEditing: Pass this back

通过在 ViewControllerB 上创建按钮的导出并设置 dataPassed = textField.text,我能够成功地将数据传递回 ViewController 。我能够让它工作的另一种方法是通过将 unwindToThisController 中的 dataReceived = sourceViewController.dataPassed 更改为 dataReceived = sourceViewController.textField.text功能。但上面提到的已接受答案的发布者似乎没有使用这些解决方法,而且我不确定这些解决方法中的任何一个是否是可以接受的方法。

我使用的是 Xcode 8.2.1 和 Swift 3。

ViewController代码(初始 View Controller )

/* ViewController.swift */
import UIKit

class ViewController: UIViewController {

var dataReceived: String? {
willSet {
print("newValue: " + newValue!)
labelOne.text = newValue
}
}

@IBOutlet weak var labelOne: UILabel!

@IBAction func buttonOne(_ sender: UIButton) {
performSegue(withIdentifier: "viewNext", sender: self)
}

override func viewDidLoad() {
super.viewDidLoad()
labelOne.text = "Default passed data"
}

// Segue ViewController -> ViewControllerB
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "viewNext" {
let viewControllerB = segue.destination as! ViewControllerB
viewControllerB.dataPassed = labelOne.text
}
}

// Segue ViewController <- ViewControllerB
@IBAction func unwindToThisControllerWithSegue(sender: UIStoryboardSegue) {
if let sourceViewController = sender.source as? ViewControllerB {
print("dataPassed from ViewControllerB: " + sourceViewController.dataPassed!)
dataReceived = sourceViewController.dataPassed
//dataReceived = sourceViewController.textField.text
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

ViewControllerB 代码(第二个 View Controller )

/* ViewControllerB.swift */
import UIKit

class ViewControllerB: UIViewController, UITextFieldDelegate {

var dataPassed: String?

@IBOutlet weak var textField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
textField.text = dataPassed
textField.delegate = self
}

// Text field delegate methods
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}

func textFieldDidEndEditing(_ textField: UITextField) {
print("textFieldDidEndEditing: " + textField.text!)
dataPassed = textField.text
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

}

最佳答案

我是这个问题的提出者,幸运的是我有该示例项目的备份,这就是我在那里找到的内容。这就是我在第一个 View Controller 中所做的

var displayValue: Double{
get{
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set{
display.text?="\(newValue)"
typing=false
}
}
@IBAction func unwind(segue: UIStoryboardSegue){
let foo = segue.sourceViewController as! viewControllerB
let a = foo.dataSent
displayValue = NSNumberFormatter().numberFromString(a!)!.doubleValue
}

并且运行良好。也许如果您可以分享您正在开发的示例项目,我可以明白为什么它不起作用? :)

关于ios - 使用展开转场从文本字段传递数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41619695/

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