2017-01-27 18 views
7

Używam iCarousel i muszę utworzyć własny przycisk. Chcę przekazać dane z przycisku zaprogramowanego programowo do innego widoku, ale nie mam identyfikatora segue, ponieważ programistycznie utworzyłem przycisk. Nie wiem, czy możliwe jest programowe utworzenie identyfikatora pliku.Programowo i szybko przesuwane Segue i Button

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 
     } 

    } 
+0

Możliwa duplikat [przycisk Dodawanie programowo wykonać segue] (http://stackoverflow.com/questions/21241194/adding-button-programmatically-to-perform-segue) –

Odpowiedz

11

Tworzenie seuge

Create Seuge

identyfikator przypisać

enter image description here

a przycisk cel

@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 
     } 
    } 
} 
+0

OP nie korzysta z storyboardu dla segue, dynamicznie tworzy przycisk ... –

+0

segue nie jest tworzona na przycisku kliknij, to wywołanie, gdy napisz self.performSegue (withIdentifier: "segueToNext", nadawca: self) –

+0

@harshalvalanda dziękuję! doskonały – user1781908

0

w twoim przypadku, jeśli używasz self.present i chcesz wysłać dane między widokami. Spróbuj tego:

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

nie wiem jak ustawić identyfikator Segue, ale myślę, że powyższy kod może pomóc

Jeśli chcesz zrobić do łatwiejszej pracy, można utworzyć segue w IB (interfejs Builder) i ustaw jego identyfikator, a następnie użyć

performSegue:withIdentifier:sender