我正在使用UIPageViewController
编写iOS应用。我的根视图控制器是ViewController
,我在其下添加了UIPageViewController
作为子VC。
最初,我将ViewController
设为页面视图VC委托:UIPageViewControllerDelegate
:
extension ViewController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
// set the pageControl.currentPage to the index of the current viewController in pages
if let viewControllers = pageViewController.viewControllers as? [UIViewController] {
if let viewControllerIndex = self.detailPagedVC.pages.index(of: viewControllers[0]) {
self.detailPagedVC.pageControl.currentPage = viewControllerIndex
// if current page is a single person view controller, zoom to that person's face
if let singlePersonViewController = self.detailPagedVC.pages[viewControllerIndex] as? SinglePersonPageViewController {
// print("didFinishAnimating: \(viewControllerIndex)")
self.zoomableImageVC.zoomableImageView.zoom(to: self.identificationResults[viewControllerIndex].face.rect, with: Constants.contentSpanRatio, animated: true)
} else if let summaryPageViewController = self.detailPagedVC.pages[viewControllerIndex] as? SummaryPageViewController {
self.zoomableImageVC.zoomableImageView.zoom(to: self.zoomableImageVC.zoomableImageView.imageView.bounds, with: Constants.contentSpanRatio, animated: true)
} else {
print("gw: err: unkown type of page controller in paged view ")
}
}
}
}
}
这样可以正常工作。直到我决定将委托功能移到专用的类上为止:
class PeoplePageViewDelegate: NSObject, UIPageViewControllerDelegate{
private struct Constants {
// the ratio of the content (e..g face) taken inside the entire view
static let contentSpanRatio: CGFloat = 0.8
}
// store a reference to the object which will take the actual action
// action 1: zooming
weak var zoomingActionTaker: ZoomableImageView?
// action 2: paging
weak var pagingActionTaker: PeoplePageViewController?
// the delegator who relies on this object
unowned let delegator: PeoplePageViewController
init(delegator: PeoplePageViewController) {
self.delegator = delegator
// wire back to delegator
// self.pagingActionTaker = delegator // gw: mind the nuance difference, you chain a weak ref on a unowned ref, what can go wrong?
super.init()
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
print("gw:0")
// set the pageControl.currentPage to the index of the current viewController in pages
if let viewControllers = pageViewController.viewControllers as? [UIViewController] {
print("gw:1")
if let viewControllerIndex = self.delegator.pages.index(of: viewControllers[0]) {
print("gw:2")
self.pagingActionTaker?.pageControl.currentPage = viewControllerIndex
// if current page is a single person view controller, zoom to that person's face
if let singlePersonViewController = self.delegator.pages[viewControllerIndex] as? SinglePersonPageViewController {
print("gw:3")
self.zoomingActionTaker?.zoom(to: singlePersonViewController.identification.face.rect, with: Constants.contentSpanRatio, animated: true)
} else if let summaryPageViewController = self.delegator.pages[viewControllerIndex] as? SummaryPageViewController,
let entireImageBounds = self.zoomingActionTaker?.imageView.bounds {
print("gw:4")
self.zoomingActionTaker?.zoom(to: entireImageBounds, with: Constants.contentSpanRatio, animated: true)
} else {
print("gw: err: unkown type of page controller in paged view ")
}
}
}
}
}
这会导致问题:尽管我确实将委托设置为这个新类的实例,但根本没有调用函数pageViewController(_: didFinishAnimating:previousViewControllers:transitionCompleted)
。
我唯一能想到的是,新的委托对象不再是VC(它曾经是我的主要VC)。因此,我怀疑这种行为变化与VC层级有关吗?
我错过了什么吗?
答案 0 :(得分:0)
(此答案受OOPer的评论启发)。
发现原因是因为最初我仅保留对委托对象的'at this point the dictionary is already populated similar to
'dict.Add 'ID_01', Array('val1', 'val2', 'val3')
dim i as long, arr as variant
with sheet a
arr = .range(.cells(2, "A"), .cells(.rows.count, "A").end(xlup)).value2
redim preserve arr(lbound(arr, 1) to ubound(arr, 1), 1 to 4)
'if sheet A already has values that only require updating,
'then use this instead
'arr = .range(.cells(2, "A"), .cells(.rows.count, "A").end(xlup).offset(0, 3)).value2
for i=lbound(arr, 1) to ubound(arr, 1)
If dict.exists(arr(i, 1)) Then
arr(i, 2) = dict.Item(arr(i, 1))(0)
arr(i, 3) = dict.Item(arr(i, 1))(1)
arr(i, 4) = dict.Item(arr(i, 1))(2)
end if
next i
.cells(2, "A").resize(ubound(arr, 1), ubound(arr, 2)) = arr
end with
引用。像这样:
之前:
weak
这里的问题是一旦执行点通过了class ViewController: UIViewController {
init() {
self.pageViewController.delegate = PeoplePageViewDelegate(….)
}
}
方法范围,init
就被垃圾回收了,因为它的引用计数为零。
我如何修复它:
我在PeoplePageViewDelegate
中添加了一个强引用作为字段,以便保留委托对象:
之后:
ViewController