ios - @IBAction 执行Segue

标签 ios swift xcode

我有以下内容:

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/

相关文章:

ios - 归档我的项目时,“无法导入桥接头”, 'file not found'

ios - 有没有办法以编程方式将 View 从一个按钮居中到另一个按钮?

某些客户的 iOS TCP 套接字失去连接

ios - cocoa touch 框架

iphone - 我可以在 XCode 中保留一个项目,但为 Mac OS X、Apple Mac Store 和 iOS 设备构建它吗?

ios - 带有 xcconfig 链接器标志的 CocoaPods 警告

ios - 错误: failed to launch '/private/var/containers/Bundle/Application/problem still not solved

ios - 在 Vuforia 中绘图 - 姿势矩阵

ios - Error Domain=NSURLErrorDomain Code=-1005 "网络连接丢失

ios - 为什么 Swift4 会转换一个 UIButton 数组!到 [UIButton?] 类型?