我创建了一个自定义 TabBarController 并添加了一些带有图像的按钮。 但我发现每次我加载 TabBarController 时不做任何事情并立即关闭它,内存就会永久增加。我在我的代码中找不到任何问题。请帮忙。提前致谢!
class CustomTabBarController: UITabBarController {
var menuButton: UIButton!
var announcementButton : UIButton!
var broadcastButton: UIButton!
var toDoListButton: UIButton!
var plusMenuBool = false
override func viewDidLoad() {
super.viewDidLoad()
let newInfo = UIImage(named: "info")!.imageResize(sizeChange: CGSize(width: 30, height: 30))
self.viewControllers![0].tabBarItem.image = newInfo
let newChat = UIImage(named: "chat")!.imageResize(sizeChange: CGSize(width: 30, height: 30))
self.viewControllers![1].tabBarItem.image = newChat
self.setupMiddleButton()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupMiddleButton() {
// menuButton
menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
var menuButtonFrame = menuButton.frame
menuButtonFrame.origin.y = self.view.bounds.height - menuButtonFrame.height
menuButtonFrame.origin.x = self.view.bounds.width/2 - menuButtonFrame.size.width/2
menuButton.frame = menuButtonFrame
// announcementButton
announcementButton = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
var announcementButtonFrame = announcementButton.frame
announcementButtonFrame.origin.y = self.view.bounds.height - announcementButtonFrame.height
announcementButtonFrame.origin.x = self.view.bounds.width/2 - announcementButtonFrame.size.width/2
announcementButton.frame = announcementButtonFrame
self.announcementButton.layer.anchorPoint = CGPoint(x: 0.5, y: 3.5)
announcementButton.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI*3/4))
// broadcastButton
broadcastButton = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
var broadcastButtonFrame = broadcastButton.frame
broadcastButtonFrame.origin.y = self.view.bounds.height - broadcastButtonFrame.height
broadcastButtonFrame.origin.x = self.view.bounds.width/2 - broadcastButtonFrame.size.width/2
broadcastButton.frame = broadcastButtonFrame
self.broadcastButton.layer.anchorPoint = CGPoint(x: 0.5, y: 3.5)
broadcastButton.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI))
// toDoListButton
toDoListButton = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
var toDoListButtonFrame = toDoListButton.frame
toDoListButtonFrame.origin.y = self.view.bounds.height - toDoListButtonFrame.height
toDoListButtonFrame.origin.x = self.view.bounds.width/2 - toDoListButtonFrame.size.width/2
toDoListButton.frame = toDoListButtonFrame
self.toDoListButton.layer.anchorPoint = CGPoint(x: 0.5, y: 3.5)
toDoListButton.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI+M_PI/4))
// addSubView
menuButton.layer.cornerRadius = menuButtonFrame.height/2
announcementButton.layer.cornerRadius = announcementButtonFrame.height/2
broadcastButton.layer.cornerRadius = announcementButtonFrame.height/2
toDoListButton.layer.cornerRadius = announcementButtonFrame.height/2
self.view.addSubview(menuButton)
self.view.addSubview(announcementButton)
self.view.addSubview(broadcastButton)
self.view.addSubview(toDoListButton)
menuButton.setImage(UIImage(named: "plus"), for: UIControlState.normal)
menuButton.addTarget(self, action: #selector(self.menuButtonAction(sender:)), for: UIControlEvents.touchUpInside)
announcementButton.setImage(UIImage(named: "announcement"), for: .normal)
announcementButton.addTarget(self, action: #selector(self.announcementButtonAction(sender:)), for: UIControlEvents.touchUpInside)
broadcastButton.setImage(UIImage(named: "broadcast"), for: .normal)
broadcastButton.addTarget(self, action: #selector(self.broadcastButtonAction(sender:)), for: UIControlEvents.touchUpInside)
toDoListButton.setImage(UIImage(named: "toDoList"), for: .normal)
toDoListButton.addTarget(self, action: #selector(self.toDoListButtonAction(sender:)), for: UIControlEvents.touchUpInside)
self.view.layoutIfNeeded()
}
}
最佳答案
查看 memory management in swift .
To make sure that instances don’t disappear while they are still needed, ARC tracks how many properties, constants, and variables are currently referring to each class instance. ARC will not deallocate an instance as long as at least one active reference to that instance still exists.
简而言之,您可以执行以下操作之一:
- 检查 tabBar 是否被释放。简单地驳回并不能保证这一点。
- 即使取消分配 tabBar,您也不会立即看到内存减少。
- 检查是否有其他对象引用了 tabBar。这可能会导致 tabBar 挂起,因为 ARC 会认为您可能需要引用。
关于iphone - Swift TabBarController 没有正确解除分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42386764/