loadView()抛出EXC_BAD_ACCESS内存错误

时间:2016-05-11 19:50:55

标签: ios exc-bad-access loadview

当从tableview中选择一行时,我正在将ViewController推入堆栈:

if let cell = tableView.cellForRowAtIndexPath(indexPath){

        let genre = cell.textLabel?.text ?? SelectGenreTableViewController.genres[0]; // nil coalsing trtary operand, if text desnt exist assign first value or static array belining to class not instance
        let vc = AddCommentsViewController();
        vc.genre = genre;
        navigationController?.pushViewController(vc, animated: true);

这个新的视图控制器视图是在loadView()中以编程方式构建的:

 override func loadView() {
        // pin the text voew to all sides and use dynamoc to make font size adjustable to user

        comments = UITextView(); // BAD ACCESS THROWN HERE
        comments.translatesAutoresizingMaskIntoConstraints = false;
        comments.delegate = self;
        comments.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody);
        view.addSubview(comments);



 view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[comments]|", options: .AlignAllCenterX, metrics: nil, views: ["comments": comments]))
 view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[comments]|", options: .AlignAllCenterX, metrics: nil, views: ["comments": comments]))
 }

问题是我得到了:

  

主题1:EXC_BAD_ACCESS ....'

在loadView()的第1行初始化注释textview时出现

错误。

通过调试,我注意到loadMethod()被反复调用,最终应用程序内存不足,因此出错。

知道为什么会连续调用loadView()?

由于

1 个答案:

答案 0 :(得分:1)

忘记调用super.loadView();