gpt4 book ai didi

swift - prepareForSegue 使用 UINavigationController 切换……更好的方法?

转载 作者:可可西里 更新时间:2023-11-01 01:08:49 27 4
gpt4 key购买 nike

我有几个 segues 呈现模态视图 Controller 。

class ProfileViewController: UIViewController {
func configure(profile: Profile) {
// Some ProfileViewController specific config
}
}

class SettingsViewController: UIViewController {
func configure(settings: Settings) {
// Some SettingsViewController specific config
}
}

所以这里有一些我们都写过很多次的非常标准的代码......

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.destination {
case let nav as UINavigationController:
switch nav.topViewController {
case let pvc as ProfileViewController:

pvc.configure(profile: profile)
case let svc as SettingsViewController:

svc.configure(settings: settings)
default:
break
}
default:
break
}
}

我也试过这种方式...

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.destination {
case let nav as UINavigationController
where nav.topViewController is ProfileViewController:

(nav.topViewController as! ProfileViewController).configure(profile: profile)
case let nav as UINavigationController
where nav.topViewController is SettingsViewController:

(nav.topViewController as! SettingsViewController).configure(settings: settings)
default:
break
}
}

但我想做的是将两者结合起来......

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.destination {
case let nav as UINavigationController
where let pvc = nav.topViewController as ProfileViewController:

pvc.configure(profile: profile)
case let nav as UINavigationController
where let svc = nav.topViewController as SettingsViewController:

svc.configure(settings: settings)
default:
break
}
}

但显然这不会编译。有没有人为此找到更好的模式?


更新

在下面扩展@Serj 的回答......

extension UIStoryboardSegue {
var destinationNavTopViewController: UIViewController? {
return (destination as? UINavigationController)?.topViewController
}
}

然后

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch (segue.destination, destinationNavTopViewController) {
case let (_, pvc as ProfileViewController):

pvc.configure(profile: profile)
case let (_, svc as SettingsViewController):

svc.configure(settings: settings)
case let (ovc as OtherViewController, _):
ovc.configure…
default:
break
}
}

最佳答案

在问这个 How-to 问题时,我们可以考虑两种情况:

  1. 关于您想要实现的目标的具体用例
  2. switch 语句的更一般用例

现在对于特定的用例,当你有一个导航 Controller 时,你似乎只想在 segue.destination 上切换。您可以使用 if letguard let 轻松实现此目的,如下所示:

guard let navigationController = segue.destination as? UINavigationController else { return }
guard let topController = navigationController.topViewController else { return }
switch topController {
case let profileViewController as ProfileViewController:
print("Do something for profile view controller")
default:
break
}

现在是第二种情况,它更通用,关于如何能够在 switch 语句中同时使用两个变量。答案是元组。例如,我将提供 Apple 文档中的示例,然后为您的特定用例提供示例。

let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
print("on the x-axis with an x value of \(x)")
case (0, let y):
print("on the y-axis with a y value of \(y)")
case let (x, y):
print("somewhere else at (\(x), \(y))")
}

现在,对于如何使用您的用例实现上述目标的示例,请注意,它是一个丑陋的示例,可以改进但要点:

switch (segue.destination, (segue.destination as? UINavigationController)?.topViewController) {
case (let nav as UINavigationController, let viewController as ProfileViewController):
return
// Maybe the destination wasn't a nav maybe it was the sign in view controller and you don't care about the second variable
case (let signInController as SignInViewController, let _):
return
default:
return
}

这就是 switch 语句能够如此轻松地切换元组的强大之处。我希望我的例子能回答你的问题。我试图用所有含糊不清的内容涵盖问题的所有部分。

关于swift - prepareForSegue 使用 UINavigationController 切换……更好的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50505970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com