无法更改为具有多个 segues 的另一个视图控制器。我该如何解决?
Can not change to another view controller with multiple segues. How can I resolve it?
我在 Swift 中使用多个 segue,并且在 table 视图中有一个 UIAlertView
。 UIAlertView
显示完美。当我单击第一个选项 (Ver Mapa) 时,它会完美地更改为另一个带有 segue (go_to_mapa
) 的视图控制器 (Mapa)。但是,当我使用 segue (go_to_detalle
) 按下第二个选项 (Ver detalle) 以更改为另一个视图控制器时,它不起作用。它什么都不做。我该如何解决这个问题?
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
var refreshAlert = UIAlertController(title: "Menu", message: "Seleccione una opcion", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Ver Mapa", style: .Default, handler: { (action: UIAlertAction!) in
if (segue.identifier == "go_to_mapa") {
var svc = segue!.destinationViewController as Mapa;
svc.cuenta = self.cuenta
svc.user = self.user
svc.password = self.password
let indexPath = self.tableView.indexPathForSelectedRow();
let currentCell = self.tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
svc.Device = currentCell.detailTextLabel!.text!
svc.desc = currentCell.textLabel!.text!
self.navigationController?.pushViewController(svc, animated: false)
}
}))
refreshAlert.addAction(UIAlertAction(title: "Ver Vehiculo", style: .Default, handler: { (action: UIAlertAction!) in
if (segue.identifier == "go_to_detalle") {
let vc = segue.destinationViewController as Detalle_Vehiculo
}
}))
refreshAlert.addAction(UIAlertAction(title: "Ejecutar comandos", style: .Default, handler: { (action: UIAlertAction!) in
println("Ejecutar comandos")
}))
presentViewController(refreshAlert, animated: true, completion: nil)
}
prepareForSegue:
在展示新控制器之前调用 shorty。来自文档:
Notifies the view controller that a segue is about to be performed.
因此,您不应在该方法中显示任何警报、弹出窗口。相反,您应该 destinationViewController
并在演示前适当调整它。
相反,您应该在 tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
中显示警报,如下所示:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var refreshAlert = UIAlertController(title: "Menu", message: "Seleccione una opcion", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Ver Mapa", style: .Default, handler: { (action: UIAlertAction!) in
self.performSegueWithIdentifier("go_to_mapa", sender:self)
}))
refreshAlert.addAction(UIAlertAction(title: "Ver Vehiculo", style: .Default, handler: { (action: UIAlertAction!) in
self.performSegueWithIdentifier("go_to_detalle", sender:self)
}))
refreshAlert.addAction(UIAlertAction(title: "Ejecutar comandos", style: .Default, handler: { (action: UIAlertAction!) in
println("Ejecutar comandos")
}))
presentViewController(refreshAlert, animated: true, completion: nil)
}
并准备:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "go_to_mapa" {
var svc = segue!.destinationViewController as Mapa
svc.cuenta = self.cuenta
svc.user = self.user
svc.password = self.password
let indexPath = self.tableView.indexPathForSelectedRow();
let currentCell = self.tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
svc.Device = currentCell.detailTextLabel!.text!
svc.desc = currentCell.textLabel!.text!
}
else if segue.identifier == "go_to_detalle" {
let vc = segue.destinationViewController as Detalle_Vehiculo
}
}
我在 Swift 中使用多个 segue,并且在 table 视图中有一个 UIAlertView
。 UIAlertView
显示完美。当我单击第一个选项 (Ver Mapa) 时,它会完美地更改为另一个带有 segue (go_to_mapa
) 的视图控制器 (Mapa)。但是,当我使用 segue (go_to_detalle
) 按下第二个选项 (Ver detalle) 以更改为另一个视图控制器时,它不起作用。它什么都不做。我该如何解决这个问题?
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
var refreshAlert = UIAlertController(title: "Menu", message: "Seleccione una opcion", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Ver Mapa", style: .Default, handler: { (action: UIAlertAction!) in
if (segue.identifier == "go_to_mapa") {
var svc = segue!.destinationViewController as Mapa;
svc.cuenta = self.cuenta
svc.user = self.user
svc.password = self.password
let indexPath = self.tableView.indexPathForSelectedRow();
let currentCell = self.tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
svc.Device = currentCell.detailTextLabel!.text!
svc.desc = currentCell.textLabel!.text!
self.navigationController?.pushViewController(svc, animated: false)
}
}))
refreshAlert.addAction(UIAlertAction(title: "Ver Vehiculo", style: .Default, handler: { (action: UIAlertAction!) in
if (segue.identifier == "go_to_detalle") {
let vc = segue.destinationViewController as Detalle_Vehiculo
}
}))
refreshAlert.addAction(UIAlertAction(title: "Ejecutar comandos", style: .Default, handler: { (action: UIAlertAction!) in
println("Ejecutar comandos")
}))
presentViewController(refreshAlert, animated: true, completion: nil)
}
prepareForSegue:
在展示新控制器之前调用 shorty。来自文档:
Notifies the view controller that a segue is about to be performed.
因此,您不应在该方法中显示任何警报、弹出窗口。相反,您应该 destinationViewController
并在演示前适当调整它。
相反,您应该在 tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
中显示警报,如下所示:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var refreshAlert = UIAlertController(title: "Menu", message: "Seleccione una opcion", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Ver Mapa", style: .Default, handler: { (action: UIAlertAction!) in
self.performSegueWithIdentifier("go_to_mapa", sender:self)
}))
refreshAlert.addAction(UIAlertAction(title: "Ver Vehiculo", style: .Default, handler: { (action: UIAlertAction!) in
self.performSegueWithIdentifier("go_to_detalle", sender:self)
}))
refreshAlert.addAction(UIAlertAction(title: "Ejecutar comandos", style: .Default, handler: { (action: UIAlertAction!) in
println("Ejecutar comandos")
}))
presentViewController(refreshAlert, animated: true, completion: nil)
}
并准备:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "go_to_mapa" {
var svc = segue!.destinationViewController as Mapa
svc.cuenta = self.cuenta
svc.user = self.user
svc.password = self.password
let indexPath = self.tableView.indexPathForSelectedRow();
let currentCell = self.tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
svc.Device = currentCell.detailTextLabel!.text!
svc.desc = currentCell.textLabel!.text!
}
else if segue.identifier == "go_to_detalle" {
let vc = segue.destinationViewController as Detalle_Vehiculo
}
}