gpt4 book ai didi

ios - 协议(protocol)不适用于容器

转载 作者:行者123 更新时间:2023-11-29 01:15:05 25 4
gpt4 key购买 nike

我有一个 ViewController 和两个重叠的 ContainerViews。从其中一个 subview ,我想将选项卡更改为另一个。

enter image description here

我创建了一个协议(protocol):

protocol pickerDelegate {
func changeMainContainer(index:Int)
}

在 subview 中:

class ChildViewInContainerController: UIViewController, UIPickerViewDelegate {
var delegateStorePicker:storePickerDelegate?
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.delegatePicker?.changeMainContainer(0)
}
}

在主视图中:

class MainViewController: UIViewController, storePickerDelegate {
func changeMainContainer(index:Int) {
switch index {
case 0 :
self.container1.hidden = true
self.container2.hidden = false
case 1:
self.container1.hidden = false
self.container2.hidden = true
default: break;
}
}
}

代码有效,但未调用 changeMainContainer 函数。

最佳答案

协议(protocol):(注意我更改了符合 class 的协议(protocol))

protocol PickerDelegate: class {
func changeMainContainer(index:Int)
}

ChildView:(将您的委托(delegate)更改为弱变量)

class ChildViewInContainerController: UIViewController, UIPickerViewDelegate {
weak var delegateStorePicker: PickerDelegate?
}

主视图:

class MainViewController: UIViewController, PickerDelegate {

// I don't know how you create your view
func createChild() {
let controller = ChildViewInContainerController()
controller.delegateStorePicker = self

// Your own implementation
}
}

使委托(delegate)变弱会阻止强引用循环。

关于ios - 协议(protocol)不适用于容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35279766/

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