当从 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() 会被连续调用吗?
谢谢