【问题标题】:Add two guard let in a prepare for segue function在准备转场功能中添加两个守卫
【发布时间】:2018-12-03 03:21:13
【问题描述】:

我正在尝试使用三个分层的 tableView 构建我的第一个应用程序。中间的 VC 有两个守卫合而为一,为 segue 功能做准备。

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        guard let destination = segue.destination as? AddMemberViewController else {
            return
        }
        destination.club = club

        guard let Destination = segue.destination as? TransactionViewController, let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row else {
            return
        }
        Destination.member = members[selectedRow]

    }

这就是它的样子。我可以解决这个问题吗,这两个函数都被我的应用程序使用,因为它只使用顶部的函数。

【问题讨论】:

    标签: swift xcode segue


    【解决方案1】:

    问题是如果你想继续到 TransactionViewController 函数已经返回,因为 segue.destination 不是 AddMemberViewController。

    相反,您应该为您的 segues 提供不同的标识符,并在 prepareForSegue 中请求它们。像这样的:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        if segue.identifier == "AddMemberVCSegue" {
            guard let destination = segue.destination as? AddMemberViewController else {
                return
            }
            destination.club = club
        }
    
        if segue.identifier == "TransactionVCSegue" {
            guard let Destination = segue.destination as? TransactionViewController, let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row else {
                return
            }
            Destination.member = members[selectedRow]
        }
    }
    

    【讨论】:

    • 好吧,我要试试这个。非常感谢:D
    • @TheClubApp 如果它正在工作并且你喜欢我的回答,如果你接受我的回答,我将不胜感激:)
    • 很高兴我能帮上忙 :)
    【解决方案2】:

    只需用 if let 替换守卫:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? AddMemberViewController {
           destination.club = club
        } else if let destination = segue.destination as? TransactionViewController, let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row {
           destination.member = members[selectedRow]
        }     
    }
    

    【讨论】:

    • 非常感谢您的帮助:D
    【解决方案3】:

    prepare(for 被调用用于 一个 特定的 segue,因此一个接一个地执行两个 guard let 是不必要的昂贵。

    通常您使用switch 语句检查segue 的identifier,将文字标识符替换为您的实际值

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      switch segue.identifier {
         case "memberSegue":
            let destination = segue.destination as! AddMemberViewController
            destination.club = club
         case "transactionSegue":
            guard let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row else { return }
            let destination = segue.destination as! TransactionViewController
            destination.member = members[selectedRow]
         default: break
      }
    }
    

    【讨论】:

    • 非常感谢您的快速回答。
    猜你喜欢
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    相关资源
    最近更新 更多