iphone - Swift TabBarController 没有正确解除分配?

标签 iphone swift xcode

我创建了一个自定义 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.

简而言之,您可以执行以下操作之一:

  1. 检查 tabBar 是否被释放。简单地驳回并不能保证这一点。
  2. 即使取消分配 tabBar,您也不会立即看到内存减少。
  3. 检查是否有其他对象引用了 tabBar。这可能会导致 tabBar 挂起,因为 ARC 会认为您可能需要引用。

关于iphone - Swift TabBarController 没有正确解除分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42386764/

相关文章:

iphone - 如何从 Xcode 项目中删除 iPhone 5 兼容性?

objective-c - Sprite Kit 中的 iAd 暂停场景

ios - 将数据从第一个 VC 传递到第三个 (Swift)

ios - 应用内电子邮件将发送电子邮件但不会在 Xcode 中关闭

c++ - 链接器命令失败 : duplicate symbol _main

iphone - 同一个 ViewController 的 2 个 View

iphone - 弹出框的最大尺寸

iphone - 我可以使用 switch 语句的哪些替代方法来更新我的 UITableViewCells?

ios - 从 iPhone 到 Raspberry Pi 的有线连接

ios - 动态添加各种​​控件到scrollview(iOS/Swift)