[英]“message sent to deallocated instance” - Can't find a way to retain.
我有一个带分页控件的滚动视图。 它根据数组加载多个页面,每个页面都有其表视图。
for(id name in categories) {
NSLog(@"Loading Page.%i",i);
NSLog(@"categories count:%i",[categories count]);
TasksPageViewController *tasks = [[TasksPageViewController alloc] init] ;
tasks = [self.storyboard instantiateViewControllerWithIdentifier:@"TasksPageViewController"];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * i;
frame.origin.y = 0;
tasks.view.frame = frame;
[tasks populateWithData:(i-1) categoryName:name];
[self.scrollView addSubview:tasks.view];
i++;
}
.h文件是:
#import <UIKit/UIKit.h>
#import "MainPageViewController.h"
#import "TasksPageViewController.h"
@interface ViewController : UIViewController{
UIScrollView *scrollView;
IBOutlet UIPageControl *pageControl;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- (IBAction)changePage;
@end
UITableView虽然正在返回
*** -[TasksPageViewController tableView:numberOfRowsInSection:]: message sent to deallocated instance 0x68efac0
(原来它是一个EXEC_BAD_ACCESS
,但得到了NSZombiesEnabled)
我不知道如何继续,我想我必须保留tasks
但如何?
将视图控制器的视图添加为您控制的视图的子视图时,需要保留控制器。 将它保留为具有retain / strong的属性,或者如果存在许多视图控制器,则将其保留为视图控制器数组
在这种情况下,将视图添加为子视图已保留它,因此您仍然可以引用表视图。 但视图控制器已被dealloc'd,这就是为什么数据源(它是UITableViewController的视图控制器)方法被发送到解除分配的实例
另外,为什么要初始化视图控制器,然后立即从故事板中实例化它?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.