gpt4 book ai didi

ios - 在 Swift 中导航时 TextField 卸载值

转载 作者:行者123 更新时间:2023-11-30 11:04:30 25 4
gpt4 key购买 nike

我有两个 ViewController,每个都有一个用于获取用户输入数字的 TextField 和两个用于计算和导航的按钮。

SecondVC 中的 TextField 实例是用户在填充 TextFields 时创建的,它存在并显示在 View 中

问题是:当您在创建对象 - Cell 之后离开 SecondView Controller 并稍后返回时,它会被设置回 cero,这不是实例 - Cell 值

class MainViewController: UIViewController {

@IBOutlet weak var A3TextField: UITextField!

@IBAction func calc(_ sender: Any) {
let A3 = Cell(name: "A3", sheet: "", value: Double(A3TextField.text!)!)
print(A3)

}

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

}
}

class SecondViewController: UIViewController {

@IBOutlet weak var B3TextField: UITextField!

@IBAction func calc2(_ sender: Any) {
let B3 = Cell(name: "B3", sheet: "", value: Double(B3TextField.text!)!)
print(B3)
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

}

}

struct Cell {

var name: String = ""
var sheet: String = ""
var value: Double = 0

init(name: String, sheet: String, value: Double) {
self.name = name
self.sheet = sheet
self.value = value
}

}

enter image description here

最佳答案

当 Controller 弹出或推送时,它们的临时属性值或对象不能保存到其他 Controller ,即使在本身 Controller 中,当推送和返回时,它们是初始值,如果你想在页面切换时保存值,建议这些解决办法:

<强>1。传递抛出属性值(一=>二)

OneViewController:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let twoVC = storyboard.instantiateViewController(withIdentifier: "TwoViewControllerID") as! twoViewController
twoVC.myStr="Controller one to controller two!"
self.present(twoVC, animated: true, completion: nil)

TwoViewController

 var myStr=String()
override func viewDidLoad() {
super.viewDidLoad()
print("get value:\(myStr)"

}

2、使用NSUerDfault保存并获取

设置值:

let userDefault = UserDefaults.standard
userDefault.set(name, forKey: "name")
userDefault.synchronize()

获取值:

let userDefault = UserDefaults.standard
let name = userDefault.object(forKey: "name") as? String

3,storybord传递值(一=>二)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
let theSegue=segue.destination as! twoViewController theSegue.myStr="other pass way" }

4,委托(delegate)传递值(二=>一)基于唐回到首页

TwoViewController:

//(1)create a delegate before class 
protocol FTVCdelegte : NSObjectProtocol{
//define method
// one method
func change(title:String)
//two method
func ChangeColoer (Coloer:UIColor)
//three method
func ChangSucces(YON:Bool)
}

//(2)create delegate object。
var delegate_zsj:FTVCdelegte?

//(3)click back button
@IBAction func backBtnAction(_ sender: UIButton) {
delegate_zsj?.change(title: "main page")
delegate_zsj?.ChangeColoer(Coloer: UIColor.red)
delegate_zsj?.ChangSucces(YON: true)
self.navigationController?.popViewController(animated: true)
}

OneViewController

//(4)inherit delegate: 
class SecondViewController: UIViewController,FTVCdelegte,ChangeBtnDelege{
...
//(5)imple delegate method

func change(title: String) { self.title = title }

func ChangeColoer(Coloer: UIColor) { self.view.backgroundColor = Coloer }

func ChangSucces(YON: Bool) { print(YON) }

//(6)when pop page use
@IBAction func tiaozhuanBtnAction(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let oneVC = storyboard.instantiateViewController(withIdentifier: "OneViewControllerID") as! oneViewController
oneVC.delegate_zsj = self
self.navigationController?.pushViewController(oneVC, animated: true)
}

5,区 block 传递值(二=>一)基于唐回到首页

TwoViewController:

//(1)define a block method:
var bbchange:((_ title:String,_ myColor:UIColor)->Void)?

//(2)back to front page:
@IBAction func backBtnAction(_ sender: UIButton) {
bbchange?("document",UIColor.green)
self.navigationController?.popViewController(animated: true)

}

OneViewController

//(3)next page invoke block  method
@IBAction func tiaozhuanBtnAction(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let threeVC = storyboard.instantiateViewController(withIdentifier: "twoViewControllerID") as! twoViewController
twoVC.bbchange=
{
( title:String,myColor:UIColor) in
self.title=title
self.view.backgroundColor=myColor
}
self.navigationController?.pushViewController(twoVC, animated: true)
}

关于ios - 在 Swift 中导航时 TextField 卸载值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52878509/

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