gpt4 book ai didi

swift - 从另一个 View Controller 调用 View Controller 中的函数

转载 作者:行者123 更新时间:2023-11-28 08:24:50 24 4
gpt4 key购买 nike

这是建议的委托(delegate)过程的代码...

在主视图 Controller 中...

protocol FilterDelegate: class {
func onRedFilter()
func onGreenFilter()
func onBlueFilter()
func onUnfiltered()
}

class ViewController: UIViewController, FilterDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

----

// Increase red color level on image by one.
func onRedFilter() {

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "filterSegue" {

let dest = segue.destinationViewController as! CollectionViewController
dest.filterDelegate = self

}
}

在 Collection View Controller 中...

var filterDelegate: FilterDelegate?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

print("Cell \(indexPath.row) selected")

guard let filterDelegate = filterDelegate else {
print("Filter delegate wasn't set!")
return
}

switch indexPath.row {

case 0:
filterDelegate.onRedFilter()
case 1:
filterDelegate.onGreenFilter()
case 2:
filterDelegate.onBlueFilter()
case 3:
filterDelegate.onUnfiltered()
default:
print("No available filter.")

}

现在...代码在保护 block 处停止并打印错误消息。 switch block 不会在任何单元格按下时执行。

最佳答案

您在倒数第二句话中的理论是正确的 - 当您在“子” View Controller 中调用 storyboard.instantiateViewControllerWithIdentifier 时,您实际上是在创建主视图 Controller 的全新实例。您没有获得对现有主视图 Controller 的引用,这就是您调用的方法没有任何效果的原因。

有几种方法可以实现您想要做的事情,包括 delegate pattern或使用闭包。这是使用委托(delegate)协议(protocol)的草图:

protocol FilterDelegate: class {
func onRedFilter()
func onGreenFilter()
func onBlueFilter()
func onUnfiltered()
}

class MainViewController: UIViewController, FilterDelegate {

// implement these as required
func onRedFilter() { }
func onGreenFilter() { }
func onBlueFilter() { }
func onUnfiltered() { }

// when we segue to the child view controller, we need to give it a reference
// to the *existing* main view controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let dest = segue.destination as? ChildViewController {
dest.filterDelegate = self
}
}

}

class ChildViewController: UIViewController {

var filterDelegate: FilterDelegate?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

// ...

guard let filterDelegate = filterDelegate else {
print("Filter delegate wasn't set!")
return
}

switch indexPath.row {
case 0:
filterDelegate.onRedFilter()
case 1:
filterDelegate.onGreenFilter()
case 2:
filterDelegate.onBlueFilter()
case 3:
filterDelegate.onUnfiltered()
default:
print("No available filter.")

}

}

}

另一种选择是为 MainViewController 上 child 需要调用的每个函数在 ChildViewController 上提供闭包,并在 prepareForSegue 中设置它们.不过,使用委托(delegate)似乎更简洁一些,因为在这种情况下有很多函数。

关于swift - 从另一个 View Controller 调用 View Controller 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40322877/

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