14

I am using iCarousel and I have to create my own button. I want to pass data from the button made programmatically to another view, but I don't have a segue identifier because I created the button programmatically. I don't know if it is possible to create the identifier of the segue programmatically.

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
        }

    }
Xcoder
  • 1,433
  • 3
  • 17
  • 37
Juan Jose Rodrigo
  • 429
  • 2
  • 6
  • 14
  • Possible duplicate of [Adding button programmatically to perform segue](http://stackoverflow.com/questions/21241194/adding-button-programmatically-to-perform-segue) – Brian Ogden Jan 27 '17 at 04:39

2 Answers2

21

Create seuge

Create Seuge

Assign identifier

enter image description here

and your button target

 @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
        }
    }
 }
Harshal Valanda
  • 5,331
  • 26
  • 63
  • 3
    The OP is not using the storyboard for the segue, he creates the button dynamically... – Brian Ogden Jan 27 '17 at 04:37
  • segue is not create on button click it's calling when write self.performSegue(withIdentifier: "segueToNext", sender: self) – Harshal Valanda Jan 27 '17 at 04:39
  • @user1781908 , Accept it. You didn't took button on IB. I'm suggesting you to create segue in IB and set it some identifier. When you create button in code. You need to addTarget for it's click event. And on that target you need to use `performSegue(withIdentifier: "segueIdentifer", sender: self)`. – Harshal Valanda Jan 27 '17 at 05:15
  • @harshalvalanda perfect, i need that! – Juan Jose Rodrigo Jan 27 '17 at 05:16
1

in your case, if you use self.present and you want to send data between views. Try this:

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

I don't know how to set segue's identifier but I think code above can help

If you want do to easier work, you can create segue in IB (Interface Builder) and set it's identifier, then use

performSegue:withIdentifier:sender