Prepárese para Segue en Swift
-
21-12-2019 - |
Pregunta
Me enfrento al mensaje de error:
"UIStoryboardSegue does not have a member named 'identifier'"
Aquí está el código que causa el error.
if (segue.identifier == "Load View") {
// pass data to next view
}
En Obj-C está bien usarlo así:
if ([segue.identifier isEqualToString:@"Load View"]) {
// pass data to next view
}
¿Qué estoy haciendo mal?
Solución
Esto parece deberse a un problema en el UITableViewController
plantilla de subclase.Viene con una versión del prepareForSegue
método que requeriría que desenvuelvas la transición.
Reemplace su actual prepareForSegue
funcionar con:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "Load View") {
// pass data to next view
}
}
Esta versión desenvuelve implícitamente los parámetros, por lo que debería estar bien.
Otros consejos
swift 4, swift 3
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
}
}
}
¡Creo que el problema es que tienes que usar el!identificador desagradable
tengo
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]
}
}
¡Mi entendimiento es que sin el!Solo obtienes un valor verdadero o falso
para SWIFT 2.3, SWIFT3, y SWIFT4:
Crear un Realizar Segue en DIDSSSELECTROWATINDEXEXPATH
para ex:
self.performSegue(withIdentifier: "uiView", sender: self)
Después de eso, cree una función de preparación para capturar el Destino Segue y pasar el valor:
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 =
}
}
Debe crear una variable llamada URLREC en Destino 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