执行 segue 后 Swift 3 解散

标签 swift uitableview segue

我有三个 Controller 与 segues 相连。 Controller 一是 MyNotif, Controller 二是 AddNotif, Controller 三是 SelectInterval。

我从 Controller 2 转到 Controller 3,然后再回到 Controller 2。 这是因为在 Controller 3 中,我将从 TableView 中选择一个值(字符串),该值(字符串)将插入到 Controller 2 的字段中。

当执行从 3 到 2 的 segue 时, Controller 不会被解除。这意味着如果我从 Controller 2 按下后退按钮(我不使用导航 Controller ,后退按钮是一个简单的按钮。这是代码:)

@IBAction func backBtnPressed(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

我将降落在 Controller 3 而不是 Controller 1。

总而言之,我正在努力实现这一目标:

从 Controller 1 我转到 Controller 2。从这里我可以转到 Controller 3,选择我需要的字符串,执行 segue,返回到 Controller 2(直到这里一切正常)。一旦回到 Controller 2,如果我点击后退按钮,我想回到 Controller 1(现在发生的是我回到 Controller 3)。

有点复杂的解释......希望它有点清楚。

enter image description here

这是我的转场代码: 添加通知:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "SelectRepeatInterval" {
        if let destination = segue.destination as? SelectRepeatIntervalVC {
            destination.selectedRepeatingInterval = repeatingInterval
        }
    }
}

选择间隔:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow!
    let currentCell = tableView.cellForRow(at: indexPath)! as! SelectRepetaIntervalCell
    selectedInterval = currentCell.repeatIntervalLbl.text!
    performSegue(withIdentifier: "IntervalSelected", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "IntervalSelected" {
        if let destination = segue.destination as? AddNotificationVC {
            destination.repeatingInterval = selectedInterval
        }
    }
}

最佳答案

您正在寻找的是 presentingViewController https://developer.apple.com/documentation/uikit/uiviewcontroller/1621430-presentingviewcontroller

if let destination = self.presentingViewController as? AddNotificationVC {
            destination.repeatingInterval = selectedInterval
}
dismiss(animated: true, completion: nil)

完整代码如下

tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
 // Get Cell Label
 let currentCell = tableView.cellForRow(at: indexPath)! as! SelectRepetaIntervalCell
 selectedInterval = currentCell.repeatIntervalLbl.text!
 if let destination = self.presentingViewController as? AddNotificationVC { 
   destination.<your textfield name>.text = selectedInterval 
 }
 dismiss(animated: true, completion: nil) 
}

关于执行 segue 后 Swift 3 解散,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46075378/

相关文章:

ios - 将流媒体视频(m3u8 文件)保存到图库中

ios - sendMessage 的问题 - WatchConnectivity

iphone - 登录检查后执行 Segue

ios - 如何在 Swift 的 TableView Controller 中使用图像选择器?

ios - Storyboard中自定义 UITableViewCells 的问题

ios - 图片下载后更新单元格高度

ios - 自定义单元格内对象的选择器

ios - iMessage 应用程序以编程方式扩展 View

ios - 将 didSelectItem 用于编程的 UICollectionViewCell

ios - Swift 代码组织方式 : Extensions vs. Classes and Structs