我正在使用iCarousel,并且必须创建自己的按钮。我想将数据以编程方式创建的按钮传递到另一个 View ,但是我没有segue标识符,因为我是通过编程方式创建的。我不知道是否可以通过编程方式创建segue的标识符。

button.addTarget(self, action: #selector(buttonAction3), for: .touchUpInside)
        button.setTitle("\(titulos[index])", for: .normal)
        tempView.addSubview(button)
        let myImage = UIImage(named: "modo4.png") as UIImage?
        button.setImage(myImage, for: .normal)

let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "modo") as! Modo1ViewController
self.present(viewController, animated: false, completion: nil)

if segue.identifier == "" {
        if let destination = segue.destination as? Modo1ViewController {
            destination.nomb = nombres
        }

    }

最佳答案

创建seuge

swift - Segue和Button以编程方式快速-LMLPHP

分配标识符

swift - Segue和Button以编程方式快速-LMLPHP

和你的按钮目标

 @IBAction func button_clicked(_ sender: UIButton) {
        self.performSegue(withIdentifier: "segueToNext", sender: self)
 }

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segueToNext" {
        if let destination = segue.destination as? Modo1ViewController {
            destination.nomb = nombres // you can pass value to destination view controller

            // destination.nomb = arrayNombers[(sender as! UIButton).tag] // Using button Tag
        }
    }
 }

关于swift - Segue和Button以编程方式快速,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41886592/

10-11 03:30