我一直试图解决这个错误的引用2天,但我找不到修复。
我的应用程序是一个基本待办事项列表,包含针对不同类别的多个页面(UIPageControl)和用于完成用户任务的统计信息的第一页。 (如果您有3个类别,最后会有4个页面。)
它从NSUserDefaults加载一个类别数组,可以通过UIButton访问的模态视图修改,就像在天气应用程序中的(i)一样。
页面控件由ViewController
类构建(这是主视图)。对于类别数组中的每个元素,视图控制器生成一个新的TasksPageViewController
类实例,并为其实例化一个storyboard视图。
一切都运行良好,直到我在故事板的UITableView
视图中放置TasksPageViewController
并将其dataSource链接到同一个类。
在TasksPageViewController
中实现UITableView数据源所需的类之后,它运行正常,但是如果我尝试转到模态视图来修改类别,当我按下DONE时(它应该重新加载ViewController)并重建UIPageControll的许多页面,应用程序崩溃与EXEC_BAD_ACCESS。
使用NSZombiesEnabled进行调试,我得到了:
2012-02-12 18:55:49.460 Pontinhos [25601:fe03] * - [TasksPageViewController tableView:numberOfRowsInSection:]:发送到解除分配的实例0x68c3040的消息
但我真的不知道如何继续,我不知道哪个对象已被释放我正在打电话。
脚本如下:
ViewController.h
#import <UIKit/UIKit.h>
#import "MainPageViewController.h"
#import "TasksPageViewController.h"
@interface ViewController : UIViewController{
IBOutlet UIScrollView *scrollView;
IBOutlet UIPageControl *pageControl;
NSMutableArray * views;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *views;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- (IBAction)changePage;
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
@synthesize scrollView;
@synthesize pageControl;
@synthesize views;
//carrega views que estão na array.
- (void)viewDidLoad
{
[super viewDidLoad];
//LoadCategories
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSMutableArray *categories = [[NSMutableArray alloc] initWithArray:[data objectForKey:@"categories"]];
// MAIN PAGE
MainPageViewController *mainpage = [self.storyboard instantiateViewControllerWithIdentifier:@"MainPageViewController"];
CGRect mframe = scrollView.frame;
mframe.origin.x = 0;
mframe.origin.y = 0;
mainpage.view.frame = mframe;
[self.scrollView addSubview:mainpage.view];
int i = 1;
NSLog(@"Iniciando criação de páginas!");
for(id name in categories) {
NSLog(@"Carregou página.%i",i);
NSLog(@"categories count:%i",[categories count]);
TasksPageViewController *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];
[scrollView addSubview:tasks.view];
tasks = nil;
i++;
}
self.pageControl.numberOfPages = [categories count]+1;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * ([categories count]+1), self.scrollView.frame.size.height);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (pageControlUsed)
{
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (IBAction)changePage {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.scrollView = nil;
}
@end
TasksPageViewController.h
#import <UIKit/UIKit.h>
#import "TasksTableCellPrototypes.h"
@interface TasksPageViewController : UIViewController <UITableViewDataSource>{
IBOutlet UILabel *categoryName;
IBOutlet UITableView *tableView;
//NSMutableArray *tasksData;
}
//@property (nonatomic,retain) NSMutableArray *tasksData;
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) UILabel *categoryName;
- (void) populateWithData:(int)dataId categoryName:(NSString *)name;
@end
TasksPageViewController.m
#import "TasksPageViewController.h"
@implementation TasksPageViewController
@synthesize tableView;
//@synthesize tasksData;
@synthesize categoryName;
- (void)viewDidLoad
{
[super viewDidLoad];
// NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
//tasksData = [[NSMutableArray alloc] initWithArray:[data objectForKey:@"tasks"]];
//tasksData = [[NSMutableArray alloc] initWithObjects:@"lalal",@"lololo",nil];
}
- (void) populateWithData:(int)dataId categoryName:(NSString *)name {
//self.categoryName.text = name;
}
// Número de categorias
- (NSInteger)tableView:(UITableView *)tableViewnumberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"contou table view tasks");
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TasksTableCellPrototypes *cell = [[TasksTableCellPrototypes alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"taskCell"];
return cell;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
谢谢。
编辑:显然ViewController
未保留对TasksPageViewController
的引用,而UITableView
无法找到其dataSource。我真的不知道如何使用ARC保留它。
答案 0 :(得分:0)
在ViewController viewDidLoad
中,当您遍历类别时,实例化名为tasks
的TasksPageViewController,但之后您不会在任何地方保留引用。因此,稍后在发送tableView:numberOfRowsInSection:
消息时找不到TasksPageViewController。
我在ViewController中看到你合成了一个名为views
的属性,但是你不在任何地方使用该属性。也许您应该重命名该属性viewControllers
,然后在viewDidLoad
的循环结束时,您应该将tasks
对象添加到viewControllers
可变数组,而不是设置{ {1}}为零。我认为这可以防止崩溃。
tasks