每个segue到模态vc的内存使用量都会增加

时间:2016-02-25 16:06:41

标签: ios swift sprite-kit segue unwind-segue

我有两个视图控制器,一个是第二个用于创建的时间轴。在第二个视图控制器中,我有一个子视图。此子视图是SKView。现在,每次我使用它时,它会将内存使用量增加2mb(在真实设备上),但是当我放松它时内存使用量保持不变。
所以它是这样的:我开始使用12mb,然后它得到14-15mb。放松后,它保持在14-15mb左右。在第二个变为它之后,它变为17mb ......依此类推。

这是时间轴控制器中使用的代码:

@IBAction func createButtonAct(sender: AnyObject) {

    self.performSegueWithIdentifier("create", sender: self)


}

@IBAction func unwindFromCreation(segue: UIStoryboardSegue) {


}




// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "create"{
        let vc = segue.destinationViewController as! CreateViewController
        if vc.currCountry == nil || vc.currCountry != self.currCountry{
        vc.currCountry = self.currCountry
        }

    }


}

这是创建视图控制器中的代码:

class CreateViewController: UIViewController, UITextViewDelegate {

@IBOutlet weak var bubbleView: SKView!

@IBOutlet var arrow: UIButton!

var ref: Firebase!


let categories = CatsAndColors.categories


@IBOutlet var doneButton: UIButton!

@IBOutlet var titleField: UITextField!

@IBOutlet var descriptionView: KMPlaceholderTextView!

var choosedCat: String!

var selectedCats: NSMutableArray!

var currCountry:String!

var tap: UITapGestureRecognizer!

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)

    UIApplication.sharedApplication().statusBarStyle = .Default
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        self.arrow.transform = CGAffineTransformMakeRotation(3.14159)
    })



    titleField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged) 

    titleField.addTarget(self, action: "textFieldDidBegin:", forControlEvents: UIControlEvents.EditingDidBegin)

    titleField.addTarget(self, action: "textFieldDidEnd:", forControlEvents: UIControlEvents.EditingDidEnd)
    // the targets get removed in viewWillDisappear
    selectedCats = NSMutableArray()

}



override func viewDidLoad() {
    super.viewDidLoad()

    ref = Firebase(url: "https://blabber2.firebaseio.com")

    self.doneButton.enabled = false

    doneButton.setBackgroundImage(UIImage(named: "Done button inactive"), forState: .Disabled)

    doneButton.setTitleColor(UIColor(netHex: 0xF6F6F6), forState: .Disabled)

    doneButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)

    self.setupBubbles()

    self.descriptionView.delegate = self

}

func setupBubbles(){



    let floatingCollectionScene = ChooseBubblesScene(size: bubbleView.bounds.size)

    // floatingCollectionScene.scaleMode = .AspectFit


    /*let statusBarHeight = CGRectGetHeight(UIApplication.sharedApplication().statusBarFrame)
    let titleLabelHeight = CGRectGetHeight(self.tibleLabel.frame)*/

    bubbleView.presentScene(floatingCollectionScene)



    for (category, color) in categories {

        let node = ChooseBubbleNode.instantiate()
        node!.vc = self
        node!.fillColor = SKColor(netHex: color)
        node!.strokeColor = SKColor(netHex: color)
        node!.labelNode.text = category

        floatingCollectionScene.addChild(node!)

    }


}
...

catsAndColors结构看起来像这样:

struct CatsAndColors{

static var categories = ["Crime":0x5F5068, "Travel":0xFBCB43, "Religion":0xE55555, "Tech":0xAF3151, "Economy":0x955BA5, "Games":0xE76851, "Climate":0x6ED79A, "Books":0xE54242, "History":0x287572, "Clothes":0x515151, "Sports":0x4AB3A7, "Food":0xD87171, "Politics":0x5FA6D6, "Music":0xDD2E63, "Tv-shows":0x77A7FB]

 }

2 个答案:

答案 0 :(得分:1)

也许您已在视图控制器之间创建了某种保留周期 如果两个视图控制器都相互拥有引用,则尝试将其中一个引用声明为weak

有关主题的更多信息,请阅读Resolving Strong Reference Cycles Between Class Instances

答案 1 :(得分:0)

我解决了这个问题,它是sknode文件中的强引用。

感谢您的回答。