2017-02-22 76 views
0

我創建了一個自定義的TabBarController並在它們上面添加了幾個帶有圖像的按鈕。 但我發現,每次我加載TabBarController而不做任何事情,並立即解僱它,內存永久增加。我在代碼中找不到任何問題。請幫忙。提前致謝!Swift 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

我發現tabBarController的子viewController對它的父tabBarController有一個「強」的引用。在我將它改爲弱之後,tabBarController正確解除分配!

0

結賬memory management in swift

要確保實例在仍然需要時不會消失,ARC會跟蹤當前引用每個類實例的有多少個屬性,常量和變量。只要至少有一個對該實例的活動引用仍然存在,ARC就不會解除分配實例。

總之你可以做以下的事情之一:

  1. 檢查是否使用TabBar是越來越釋放。簡單解僱並不能保證。
  2. 即使tabBar被釋放,您也不會立即看到內存減少。
  3. 檢查是否有其他對象正在引用tabBar。這可能會導致tabBar掛起,因爲ARC會認爲您可能需要引用。
+0

我確實有自己的孩子堅持參考它!將其改爲弱解決了這個問題。 –

+0

如果能解決您的問題,您能否將我的答案標記爲正確? – SagarU