gpt4 book ai didi

ios - @IBAction 执行Segue

转载 作者:行者123 更新时间:2023-11-28 16:08:59 25 4
gpt4 key购买 nike

我有以下内容:

class Settings: UIViewController {

@IBAction func CitySend(sender: AnyObject) {
self.performSegue(withIdentifier: "senddata", sender: self)
}

@IBOutlet weak var textField: UITextField!

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

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "senddata" {
let Mainview = segue.destination as! ViewController
Mainview.label = textField.text!
}
}
}

我的主视图是这样的:

class ViewController: UIViewController {

@IBOutlet var city: UILabel!
var label: String = ""


override func viewDidLoad() {
super.viewDidLoad()


city.text = label
}

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

我不确定,如果这是我应该这样做的方式,但对我来说逻辑是,如果按下按钮,我只想允许这个 segue 将字符串传递到我的主视图中的标签(就像保存按钮)

然而,至于我现在所做的,没有任何反应,每当我按下按钮时,它都会给我 1: signal SIGABRT 错误。

最佳答案

使用以下代码:

ViewController.swift

 import UIKit

class ViewController: UIViewController, SecondViewControllerProtocol{
@IBOutlet weak var dataLabel: UILabel!

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

@IBAction func navigateToSecondViewControllerOnButtonClick(_ sender: AnyObject) {

let secondVC: ViewController2 = storyboard?.instantiateViewController(withIdentifier: "viewcont2") as! ViewController2
secondVC.delegate = self
self.navigationController?.pushViewController(secondVC, animated: true)
}

func saveData(data: String){
dataLabel.text = data
}

}

ViewController2.swift

 import UIKit


protocol SecondViewControllerProtocol {
func saveData(data: String) // this function the first controllers
}

class ViewController2: UIViewController {

var delegate: SecondViewControllerProtocol?

@IBOutlet weak var dataTextField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

}

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

@IBAction func saveButtonClicked(_ sender: AnyObject) {
delegate?.saveData(data: dataTextField.text!)
}
}

Storyboard截图:

enter image description here

请检查下面我的 GitHub 链接以测试示例:

https://github.com/k-sathireddy/PassingDataThroughSegue

关于ios - @IBAction 执行Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39831712/

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