[英]prepareForSegue doesn't work in swift. How can i resolve it?
我有兩個帶有自定義“ push” Segue的viewcontrollers。 當我在tableview中選擇一行時,它完美地顯示了一個帶有三個選項的警報框。 我的問題是,當我選擇第一個選項(Ver Mapa)更改為anotherviewcontroller時,它不起作用。 什么都不做。 我該如何解決?
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;
self.performSegueWithIdentifier("go_to_mapa", sender: self)
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!
}
}))
refreshAlert.addAction(UIAlertAction(title: "Detalle Vehiculo", style: .Default, handler: { (action: UIAlertAction!) in
println("Detalle Vehiculo")
}))
refreshAlert.addAction(UIAlertAction(title: "Ejecutar comandos", style: .Default, handler: { (action: UIAlertAction!) in
println("Ejecutar comandos")
}))
refreshAlert.addAction(UIAlertAction(title: "Cancelar", style: .Default, handler: { (action: UIAlertAction!) in
self.back()
}))
presentViewController(refreshAlert, animated: true, completion: nil)
}
您的代碼搞砸了。
顯示警報的代碼不屬於prepareForSegue
。 您不應該在prepareForSegue
執行任何UI。 您也不應在prepareForSegue
內調用performSegueWithIdentifier
。 在您或用戶觸發segue之后,系統將在顯示新的視圖控制器之前調用prepareForSegue方法。 它使您有機會進行設置。
您發布的代碼不太可能顯示警報。
這是您想做的:
如果要在用戶選擇表視圖中的單元格時觸發警報,請實現表視圖委托方法didSelectRowAtIndexPath:
使用該方法,創建並顯示警報控制器。 當用戶選擇一個選項時,如果要觸發segue,則應調用performSegueWithIdentifier。
觸發segue之后,系統將調用您的prepareForSegue
方法
在動作處理程序中,您必須調用 segue,而不是對其進行檢查。 您可以在prepareForSegue
檢查prepareForSegue
...
// calling the segue
self.performSegueWithIdentifier("go_to_mapa")
我沒有動畫地解決了它:false像這樣:
...
...
svc.Device = currentCell.detailTextLabel!.text!
svc.desc = currentCell.textLabel!.text!
self.navigationController?.pushViewController(svc, animated: false)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.