iOS中的内存管理,不使用导航控制器

时间:2016-09-26 02:39:53

标签: ios swift memory-management

我是iOS开发的新手,并且在上个月我自己学习它。我做了一个简单的应用程序,其中我选择了将两个视图控制器连接到segue的简单方法,然后返回到dismissViewController

当我从一个视图转到第二个视图时,Xcode模拟器中的内存使用量从15MB增加到20MB但是当我返回到第一个时它不会变成15MB(它仍然是大约19.5 MB)。我已经经历了很多答案并尝试过它们没有任何工作。

以下是我的第二个视图控制器的代码。请提出建议。

import Foundation
import UIKit

class AboutUsView : ViewController {
    //var loginScreenView: HomeScreenView!
    var imageview = UIImageView()
    var SwiftTimer : NSTimer?
    var SwiftCounter = 0
    var images: [String]?

    override func viewDidLoad() {
        super.viewDidLoad()

        images = ["abt1", "abt2", "abt3", "abt4"]

        let homeButton = self.view.addHeaderToView()
        homeButton.addTarget(self, action: #selector(goToHomePage), forControlEvents: .TouchUpInside)

        self.addPicture(images!)
        self.addLabelHeader("About us")
        self.addLabelBody("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting")
    }

    @IBAction func goToHomePage(homeButton: UIButton) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(true)
        SwiftTimer?.invalidate()

        self.view.removeFromSuperview()
    }

    func addPicture(images:[String]) {
        // screen width and height

        let width = UIScreen.mainScreen().bounds.size.width
        let height = UIScreen.mainScreen().bounds.size.height

        imageview = UIImageView(frame: CGRectMake(0, 0.1*height, width, 0.4*height))
        imageview.image = UIImage(named: "abt1")

        SwiftTimer = NSTimer.scheduledTimerWithTimeInterval(3, target:self, selector: #selector(updateCounter), userInfo: nil, repeats: true)

        // you can change the content mode:

        imageview.contentMode = UIViewContentMode.ScaleToFill
        self.view.addSubview(imageview)
    }

    func updateCounter() {
        SwiftCounter = (SwiftCounter < 0 ? (images!.count - 1) : SwiftCounter % images!.count )

        imageview.image = UIImage(named: images![SwiftCounter++])
    }

    func addLabelHeader(labelText:String) {
        // screen width and height:

        let width = UIScreen.mainScreen().bounds.size.width
        let height = UIScreen.mainScreen().bounds.size.height

        let logoLabel = UILabel(frame: CGRectMake(0.05 * width, 0.52*height, 0.5*width, 0.05*height))
        logoLabel.text = labelText
        logoLabel.textColor = (UIColor.blackColor())
        logoLabel.font = UIFont.boldSystemFontOfSize(24.0)

        self.view.addSubview(logoLabel)
    }

    func addLabelBody(labelText:String) {
        // screen width and height:
        let width = UIScreen.mainScreen().bounds.size.width
        let height = UIScreen.mainScreen().bounds.size.height

        let logoLabel = UILabel(frame: CGRectMake(0.05 * width, 0.52*height, 0.9*width, 0.35*height))
        logoLabel.text = labelText
        logoLabel.textColor = (UIColor.blackColor())
        logoLabel.font = UIFont.systemFontOfSize(17.0)
        logoLabel.numberOfLines = 10
        self.view.addSubview(logoLabel)
    }
}

0 个答案:

没有答案