当从表视图中选择一行时,我正在将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行初始化comments textview时出错。
通过调试,我注意到loadMethod()被反复调用,最终应用程序内存不足,因此出现了错误。
知道为什么loadView()会被连续调用吗?
感谢
忘记调用super.loadView();