web-dev-qa-db-ja.com

プログラム的にセグエとボタンswift

私はiCarouselを使用しており、独自のボタンを作成する必要があります。プログラムで作成されたボタンから別のビューにデータを渡したいのですが、ボタンをプログラムで作成したため、セグエ識別子がありません。プログラムでセグエの識別子を作成できるかどうかはわかりません。

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
        }

    }
13

seugeを作成する

Create Seuge

識別子の割り当て

enter image description here

そしてあなたのボタンターゲット

 @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
        }
    }
 }
17
Harshal Valanda

あなたの場合、self.presentを使用し、ビュー間でデータを送信する場合。これを試して:

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

セグエの識別子の設定方法はわかりませんが、上記のコードが役立つと思います

作業を簡単にしたい場合は、IB(Interface Builder)でセグエを作成し、その識別子を設定してから、

performSegue:withIdentifier:sender
0