gpt4 book ai didi

ios - 关闭 ViewController 不会在 Swift 中释放内存

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

我有一个 View Controller (我们称之为 TableViewController)。当用户点击自定义 UITableViewCell 中的按钮时,将打开一个新的 View Controller (我们称之为 DetailViewcontroller)。当用户点击 DetailViewController 内的后退按钮时,我调用了 dismiss 函数,TableViewController 再次打开。

我的问题是,当我执行这种情况 50 次时,内存使用率会更高。所以我猜它不会进行任何重新分配。

Memory Usage

我如何打开 DetailViewController;

    @objc func voteBtnTapped(_ sender: UIButton){
guard let team = FirebaseManager.instance.currentTeam else { return }
FirebaseManager.instance.delegate = nil
let voteController = VoteViewController()
voteController.currentTeam = team
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
view.window!.layer.add(transition, forKey: kCATransition)
self.present(voteController, animated: false, completion: nil)
}

我如何关闭 DetailViewController;

    @objc func backBtnTapped(_ sender: UIButton){
FirebaseManager.instance.delegate = nil
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
view.window!.layer.add(transition, forKey: kCATransition)
self.dismiss(animated: false, completion: nil)
}

它说 Instruments 有泄漏,但我想不通,是什么导致了这些韭菜。

enter image description here

编辑:我发现我正在使用的选择器库(在 DetailViewController 中)会阻止释放。您知道为什么它会阻止它吗?我该如何处理它?<​​/p>

从响应到回答我如何声明 pickerView:

 let config = AZAPickerConfiguration<PickerItem>(items: (1...10).map { PickerItem(number: $0) },
defaultSelectedIndex: 4,
selectedFont: UIFont(name: "SourceSansPro-SemiBold", size: 50)!, nonSelectedFont:UIFont(name: "SourceSansPro-Light", size: 20)!, selectionRadiusInPercent: 0.5,
selectionBackgroundColor: UIColor(red:0.00, green:0.42, blue:0.20, alpha:1.0), itemWidth: 80)

self.pickerView = AZAPicker<PickerItem>(with: config, frame: .zero)

pickerView!.backgroundColor = .white
pickerView!.translatesAutoresizingMaskIntoConstraints = false
//self.pickerView!.onPickItem = self.picker
self.pickerView!.onPickItem = {( sender : AZAPicker<PickerItem>,item:PickerItem) in
print("didPickItem: \(item)")
self.currentPoint = item.number

}

https://github.com/AvanzaBank/AZAPicker

最佳答案

那个库在使用形式上好像有错误,在示例代码中声明了这样一个方法

func picker(sender: AZAPicker<PickerItem>, item: PickerItem) {
print("didPickItem: \(item)")
}

但不是这样做,您需要以这种方式分配一个闭包,问题已解决!

已更新(在您的代码中)

self.pickerView!.onPickItem = {[weak self]( sender : AZAPicker<PickerItem>,item:PickerItem) in
print("didPickItem: \(item)")
self.currentPoint = item.number

}

完整代码引用

class ViewController: UIViewController {

@IBAction func dismiss(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()

let config = AZAPickerConfiguration<PickerItem>(items: (1...100).map { PickerItem(number: $0) },
defaultSelectedIndex: 99,
selectionRadiusInPercent: 0.5,
itemWidth: 80)

let pickerView = AZAPicker<PickerItem>(with: config, frame: .zero)

pickerView.backgroundColor = .white
//in this code [weak self] is not needed because I don't use self inside the closure
pickerView.onPickItem = {( sender : AZAPicker<PickerItem>,item:PickerItem) in
print("didPickItem: \(item)")
}

pickerView.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(pickerView)

NSLayoutConstraint(item: pickerView, attribute: .top, relatedBy: .equal, toItem: topLayoutGuide, attribute: .top, multiplier: 1, constant: 20).isActive = true

NSLayoutConstraint(item: pickerView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0).isActive = true

NSLayoutConstraint(item: pickerView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true

NSLayoutConstraint(item: pickerView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 80).isActive = true
}

}

关于ios - 关闭 ViewController 不会在 Swift 中释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49853173/

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