2012-02-14 36 views
3

我有一個帶分頁控制的滾動視圖。它根據數組加載多個頁面,每個頁面都有其表格視圖。」發送到解除分配的實例的消息「 - 找不到保留的方法。

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)

我不知道如何繼續,我th墨水我必須保留tasks但是如何?

回答

6

將視圖控制器的視圖添加爲您控制的視圖的子視圖時,您需要保留控制器。將它保留爲保留/強大的屬性,或者如果其中有很多視圖控制器的數組,請將其保留,因此您仍然有對錶視圖的引用。但視圖控制器已被dealloc'd這就是爲什麼數據源(這是視圖控制器的UITableViewController)方法正在發送到一個釋放實例

此外,爲什麼你初始化視圖控制器,然後立即實例化它從故事板?

+0

這只是一個試驗,原來是'TasksPageViewController *任務= [self.storyboard instantiateViewControllerWithIdentifier:@「TasksPageViewController」];' – jturolla 2012-02-14 17:19:30

+0

很公平,我還沒有真正使用情節串連圖板,所以我主要只是好奇 – wattson12 2012-02-14 17:21:54

+0

謝謝,有效。我只是創建了一個可變數組,並將每個'tasks'添加到它中。 – jturolla 2012-02-14 17:22:27

相關問題