0

I am creating a custom keyboard. The basic principle is that start a query using FMDB and get a list of fonts (an array). After that I need to change the title of UIButtons accordingly. But after several times, memory will reach the upper limit of the custom keyboard. Then the the keyboard restart. I think below is the source of the problem. How to modify it so that it will not draw a huge memory during each execution. Please help.

func create_font_button(){

    let u = UIScreen.main.bounds.width/8

    font_view.frame = CGRect(x: 0, y:0, width:mainview.bounds.size.width, height: UIScreen.main.bounds.height / 45 * 4)
    font_view.backgroundColor = UIColor.white


    self.view.addSubview(font_view)
    for index in 0...7 {
        font_btn = UIButton(type: .roundedRect)
        font_btn.tag = index
        font_btn.backgroundColor = UIColor.yellow
        font_btn.titleLabel!.font =  UIFont(name: "Helvetica-Bold", size: 30)
        font_btn.frame = CGRect(x:u * CGFloat(index), y: 0, width: u-1, height: UIScreen.main.bounds.height / 45 * 4)

        font_btn.titleLabel?.minimumScaleFactor = 0.1
        font_btn.titleLabel?.numberOfLines = 1
        font_btn.titleLabel?.adjustsFontSizeToFitWidth = true

        if index == 0{
            if font.count == 0{
                font_view.removeFromSuperview()
            }
            else{
                font_btn.backgroundColor = UIColor(red: (255/255.0), green: (204/255.0), blue: (204/255.0), alpha: 1.0)
                font_btn.setTitle(font[0], for: .normal)
                font_btn.addTarget(self, action: #selector(font_btn_tap), for: .touchUpInside)
            }
        }
        else  if index == 1{
            if font.count < 2{
                font_btn.backgroundColor = UIColor.white
                font_btn.setTitle("", for: .normal)
            }

            else{
                font_btn.backgroundColor = UIColor(red: (196/255.0), green: (240/255.0), blue: (244/255.0), alpha: 1.0)
                font_btn.setTitle(font[1], for: .normal)
                font_btn.addTarget(self, action: #selector(font_btn_tap), for: .touchUpInside)

            }
        }
        else  if index == 2{
            if font.count < 3  {
                font_btn.backgroundColor = UIColor.white
                font_btn.setTitle("", for: .normal)

            }
            else{
                font_btn.backgroundColor = UIColor(red: (255/255.0), green: (254/255.0), blue: (204/255.0), alpha: 1.0)
                font_btn.setTitle(font[2], for: .normal)
                font_btn.addTarget(self, action: #selector(font_btn_tap), for: .touchUpInside)
            }
        }
        else{
            if font.count < index + 1{

                font_btn.backgroundColor = UIColor.white
                font_btn.setTitle("", for: .normal)
            }
            else{
                font_btn.backgroundColor = UIColor(red: 248.0/255, green: 242.0/255, blue: 227.0/255, alpha: 1)
                font_btn.setTitle(font[index], for: .normal)
                font_btn.addTarget(self, action: #selector(font_btn_tap), for: .touchUpInside)
            }
        }
        font_view.addSubview(font_btn)

    }

}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Your [previous contention](http://stackoverflow.com/q/42982930/1271826) was that it was the SQLite code that was consuming memory. Now you suggest it's the button code. I'm not convinced the problem is here, either (though in the above code, you're not showing us where you create `font_view` and where you remove the prior `font_view`; if you don't remove the old one and just add new ones, you'll end up with abandoned memory. – Rob Mar 25 '17 at 19:42
  • But I'd like to step back and ask how precisely you concluded that the above code was the source of the problem. It feels like you're guessing rather than using "Debug Memory Graph" or Instruments (see http://stackoverflow.com/a/30993476/1271826) to identify what's leaking and how much memory it takes up. But even if the above code was leaking views and buttons, that memory consumption isn't that dramatic. Usually it takes images or the like to consume a lot of memory. – Rob Mar 25 '17 at 19:42
  • Thanks for your comment. AS I am green in programming, please help to find out the source of the problem. It irritates me a very long time. Below please find my source code for your reference. Really really thank you for your help. https://drive.google.com/open?id=0B0hgQ2JiGj8tMjFSaWZXak9oTUU – user2174638 Mar 26 '17 at 14:41

0 Answers0