Préparer Segue dans Swift
-
21-12-2019 - |
Question
Je suis confronté au message d'erreur:
"UIStoryboardSegue does not have a member named 'identifier'"
Voici le code à l'origine de l'erreur
if (segue.identifier == "Load View") {
// pass data to next view
}
Sur l'Obj-C, c'est bien en utilisant comme ceci:
if ([segue.identifier isEqualToString:@"Load View"]) {
// pass data to next view
}
Ce que je fais mal?
La solution
Ceci semble être dû à un problème dans le UITableViewController
sous-classe de modèle.Il est livré avec une version de la prepareForSegue
méthode qui vous obligeraient à déballer la séquence.
Remplacer votre actuel prepareForSegue
fonction avec:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "Load View") {
// pass data to next view
}
}
Cette version implicitement déballe les paramètres, de sorte que vous devriez être bien.
Autres conseils
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MySegueId" {
if let nextViewController = segue.destination as? NextViewController {
nextViewController.valueOfxyz = "XYZ" //Or pass any values
nextViewController.valueOf123 = 123
}
}
}
Je pense que le problème est que vous devez utiliser le!à l'identifiant pour incrédule
j'ai
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
if segue!.identifier == "Details" {
let viewController:ViewController = segue!.destinationViewController as ViewController
let indexPath = self.tableView.indexPathForSelectedRow()
viewController.pinCode = self.exams[indexPath.row]
}
}
Ma compréhension est que sans le!Vous venez d'obtenir une valeur vraie ou fausse
pour SWIFT 2.3, SWIFT3 et SWIFT4:
Créer une SEGUE EFFAIRE À DIDSELLECTROWATINDEXPATH
pour ex:
self.performSegue(withIdentifier: "uiView", sender: self)
Après cela crée une fonction PREPARYFORSEGUE pour attraper la Segue de destination et transmettre la valeur:
ex:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "uiView"{
let destView = segue.destination as! WebViewController
let indexpath = self.newsTableView.indexPathForSelectedRow
let indexurl = tableDatalist[(indexpath?.row)!].link
destView.UrlRec = indexurl
//let url =
}
}
Vous devez créer une variable nommée URLREC dans ViewController
Swift 1.2
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "ShowDeal") {
if let viewController: DealLandingViewController = segue.destinationViewController as? DealLandingViewController {
viewController.dealEntry = deal
}
}
}
Prepare for Segue in Swift 4.2 and Swift 5.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "OrderVC") {
// pass data to next view
let viewController = segue.destination as? MyOrderDetailsVC
viewController!.OrderData = self.MyorderArray[selectedIndex]
}
}
How to Call segue On specific Event(Like Button Click etc):
performSegue(withIdentifier: "OrderVC", sender: self)
this is one of the ways you can use this function, it is when you want access a variable of another class and change the output based on that variable.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let something = segue.destination as! someViewController
something.aVariable = anotherVariable
}
Provided you aren't using the same destination view controller with different identifiers, the code can be more concise than the other solutions (and avoids the as!
in some of the other answers):
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if let myViewController = segue.destinationController as? MyViewController {
// Set up the VC
}
}
Change the segue identifier in the right panel in the section with an id. icon to match the string you used in your conditional.
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
if(segue!.identifier){
var name = segue!.identifier;
if (name.compare("Load View") == 0){
}
}
}
You can't compare the the identifier with == you have to use the compare() method