我创建了一个自定义的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()
}
}
答案 0 :(得分:0)
为了确保实例在仍然需要时不会消失,ARC会跟踪当前引用每个类实例的属性,常量和变量的数量。只要至少有一个对该实例的活动引用仍然存在,ARC就不会解除分配实例。
简而言之,您可以执行以下操作之一: