我正在像我的prepareForSegue函数中初始化容器视图控制器:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
initialiseTopViewController(for: segue)
initialiseMessagesViewController(for: segue)
initialiseToolbarViewController(for: segue)
initialiseTicketViewController(for: segue)
}
由于segue标识符上的guard
条件,每个segue实际上只能运行这些功能之一。我该如何做,以便如果执行其中一个功能,而不会继续尝试执行其他功能?有没有比仅使用if...else if
块更好的方法
答案 0 :(得分:1)
一种解决方案是,如果每个initialise
函数处理完序列,则返回true
,否则返回false
。然后:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
_ = (false
|| initialiseTopViewController(for: segue)
|| initialiseMessagesViewController(for: segue)
|| initialiseToolbarViewController(for: segue)
|| initialiseTicketViewController(for: segue)
)
}
private func initialiseTopViewController(for segue: UIStoryboardSegue) -> Bool { ... }
private func initialiseMessagesViewController(for segue: UIStoryboardSegue) -> Bool { ... }
private func initialiseToolbarViewController(for segue: UIStoryboardSegue) -> Bool { ... }
private func initialiseTicketViewController(for segue: UIStoryboardSegue) -> Bool { ... }
另一种解决方案是将适当的测试提升到prepare(for:sender:)
中,例如使用switch
:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch (segue.identifier ?? "", segue.destination) {
case (_, let dest as TopViewController): initialiseTopViewController(dest)
case (_, let dest as MessagesViewController): initialiseMessagesViewController(dest)
case ("toolbar", let dest as MultipurposeViewController): initialiseToolbarViewController(dest)
case ("ticket", let dest as MultipurposeViewController): initialiseTicketViewController(dest)
default: super.prepare(for: segue, sender: sender)
}
}
private func initialiseTopViewController(_ controller: TopViewController) { ... }
private func initialiseMessagesViewController(_ controller: MessagesViewController) { ... }
private func initialiseToolbarViewController(_ controller: MultipurposeViewController) { ... }
private func initialiseTicketViewController(_ controller: MultipurposeViewController) { ... }
答案 1 :(得分:1)
类似于@rob mayoff的建议,让每个函数返回Bool
并循环遍历包含所有函数的数组,从而使其完全由数据驱动。
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let funs = [ initialiseTopViewController(for:),
initialiseMessagesViewController(for:),
initialiseToolbarViewController(for:),
initialiseTicketViewController(for:) ]
for fun in funs {
if fun(segue) {
break
}
}
}
答案 2 :(得分:0)
我建议使用switch
语句检查/广播destination
的各种类型,并将该类型的值传递给适当的方法:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.destination {
case let destination as FooViewController: prepare(destination)
case let destination as BarViewController: prepare(destination)
case let destination as BazViewController: prepare(destination)
default: super.prepare(for: segue, sender: sender)
}
}
func prepare(_ destination: FooViewController) {
destination.foo = foo
}
func prepare(_ destination: BarViewController) {
destination.bar = bar
}
func prepare(_ destination: BazViewController) {
destination.baz = baz
}
此switch
仅调用适当的方法,并将那些单独的方法保存为根本不需要执行任何guard
语句。
答案 3 :(得分:0)
我想通过使函数返回bool
// make each function return boolean values so you know if it executed properly or not based on the return value
func initialiseTopViewController(for segue: UIStoryboardSegue) -> Bool { ... }
func initialiseMessagesViewController(for segue: UIStoryboardSegue) -> Bool { ... }
func initialiseToolbarViewController(for segue: UIStoryboardSegue) -> Bool { ... }
func initialiseTicketViewController(for segue: UIStoryboardSegue) -> Bool { ... }
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// if any function returns true, then return and don't run the remaining functions
if initialiseTopViewController(for: segue) { return }
if initialiseMessagesViewController(for: segue) { return }
if initialiseToolbarViewController(for: segue) {return }
if initialiseTicketViewController(for: segue) { return }
}
很抱歉,如果这与其他建议类似。我只是想向您展示如何使用内联if语句轻松实现目标。