【问题标题】:Can not change to another view controller with multiple segues. How can I resolve it?无法更改为具有多个 segue 的另一个视图控制器。我该如何解决?
【发布时间】:2015-05-09 15:34:09
【问题描述】:

我在 Swift 中使用了多个 segue,并且我在表格视图中有一个 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)
}   

【问题讨论】:

    标签: ios swift uitableview uistoryboardsegue


    【解决方案1】:

    prepareForSegue: 在展示新控制器之前调用 shorty。来自文档:

    通知视图控制器即将执行转场。

    因此,您不应该在该方法中显示任何警报、弹出窗口。相反,您应该在演示前使用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
        }
    }
    

    【讨论】:

    • 它在地图视图控制器上显示错误,因为我将一些数据传递给地图视图控制器
    • 如果您删除了所有实际导航到任何otherviewcontroller 的代码,怎么会发生这种情况?请确保您既没有实现tableView:didSelectRowAtIndexPath: 也没有实现prepareForSegue:sender:,并且当您单击表格行时没有任何反应。如果仍有问题,请也删除所有代码
    • 但是当我使用上面显示的代码时,警告框在表格视图中完美显示。当我单击“ver Mapa”时,效果很好。问题是当我点击“Ver vehiculos”时。
    • 不要再使用您发布的代码了。当点击表格时没有任何反应时,从您的来源状态,请再次将我的所有代码从答案中复制并粘贴到您的项目中。它应该可以根据需要工作
    • 第一次发帖后我更新了好几次,可能你复制了一个早期版本
    猜你喜欢
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    相关资源
    最近更新 更多